我正在从事一个涉及从两个不同服务器获取信息的项目。我打算做的是让用户输入他或她的用户名密码,然后用php脚本填写名字姓等下面的其余字段。我进行了一些搜索,发现我拥有的一些数据在服务器中的两个不同表。下面是我到目前为止的编码。

 <?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>

最佳答案

使用'JOIN'将是提取数据多表的最佳选择...

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

关于php - 从2个不同的MySQL表获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9632737/

10-13 06:27