简体   繁体   English

从2个不同的MySQL表中获取数据

[英]Getting Data from 2 different MySQL tables

I'm working on a project that involved getting information from two different servers. 我正在开发一个涉及从两个不同服务器获取信息的项目。 What i plan on doing is having the user enter his or her username password and then have a php script fill in the rest of the fields below first name last name etc. I did some searching and found that some of the data i have is on two different tables within the server. 我打算做的是让用户输入他或她的用户名密码,然后用PHP脚本填写下面名字姓氏等下面的其他字段。我做了一些搜索,发现我的一些数据是服务器中的两个不同的表。 Below is the coding I have so far. 下面是我到目前为止的编码。

 <?php
    $connect = mysql_connect("localhost","**************","**********") or die ("Couldn't Connect"); //host,username,password
    mysql_select_db("*******") or die ("Could not find database");

    $query = mysql_query("SELECT * FROM jos_users WHERE username='$username'");


    ?>
    <html>
    <form action="populate.php" method='post'>
     <table>
    <tr>
            <td>VAE&nbsp;Username:</td>

            <td><input type='text' name='username' value=''></td>
          </tr>
          <tr>
            <td>VAE&nbsp;Password:</td>

            <td><input type='password' name='password' value=''></td>
          </tr>
     </table>
      <p><input type='submit' name='submit' value='Search & Populate!'></p>
</form>
//below is the information i want filled in from the MYSQL tables
    <hr>
    $query = mysql_query("SELECT * FROM jos_users WHERE username='$username'");
    <form action="dafreg.php" method='post'>
    <table>
    <tr>
    <td>Fristname:</td>
    <td><input type='text' name='firstname' value='<?php echo $firstname; ?>'></td>
    </tr>
    <tr><td>Lastname:</td>
    <td><input type='text' name='lastname' value='<?php echo $lastname; ?>'></td>
    </tr>
    <tr>
    <td>Login:</td>
    <td><input type='text' name='login' value='<?php echo $username; ?>'></td>
    </tr>
    <tr><td>Password:</td>
    <td><input type='text' name='pass' value=''></td>
    </tr>
    <tr><td>Country:</td>
    <td><input type='text' name='country' value=''></td>
    </tr>
    <tr><td>Pilot:</td>
    <td><input type='checkbox' name='pilot' value=''></td>
    </tr>
    <tr><td>ATC:</td>
    <td><input type='checkbox' name='atc' value=''></td>
    </tr>
    <tr><td>Email:</td>
    <td><input type='text' name='email' value=''></td>
    </tr>
     </table>
     <p><input type='submit' name='submit' value='Register'></p>
     </form>





    </form>


    </html>

If the two tables are on the same database you could left join the two tables based on a common factor (probably username in this case) 如果这两个表位于同一个数据库中,您可以根据公共因子(在这种情况下可能是用户名)将两个表连接起来

SELECT *
FROM jos_users
LEFT JOIN table_name2
ON jos_users.username=table_name2.username
WHERE jos_users.username = $username

Using 'JOIN' will be the best option for extracting the data multiple table... 使用'JOIN'将是提取数据多表的最佳选择...

SELECT tab1.*,tab2.*
FROM table1 tab1 JOIN table2 tab2
ON tab1.id=tab2.id
WHERE tab1.username=$username

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM