如何使用另一个表从表中查找值

时间:2014-11-13 11:29:35

标签: php html mysql

我对表中的查找数据有疑问,... 有一个表可以保存用户的4个字段:id,name,username,email 我有另一个表,用户可以节省10个以上不需要的字段:phone,and ...

现在我想从表2 中的表1 中查找ID,并在表格中显示电话号码。 注意:ID是表1和表1之间的共享代码。 2

<?php
$con=mysqli_connect("localhost","blah","blah","blah");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Here is 2 tables
mysqli_query($con, "SET NAMES 'utf8'");
$result = mysqli_query($con,"SELECT * FROM q7fbn_users");
$result2 = mysqli_query($con,"SELECT * FROM q7fbn_user_profiles");

echo "<table border='1', table style='float:center' align='center'>
<tr>
// Here is the table 1 fields
<th>ID</th>
<th>Name</th>
<th>Username</th>
<th>E-mail</th>

// Here is the phone number that i want lookup from table 2
<th>Phone</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
echo "<tr align='center'>";
// Here is the table 1 fields
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
// Here is the phone number that i want lookup from table 2
  echo "<td>" . $row['phone'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

1 个答案:

答案 0 :(得分:0)

您的问题是,在您的行中只有来自用户表的数据,因为您只从$result获取数据。 你需要加入他们:

$result = mysqli_query($con, "SELECT * FROM q7fbn_users LEFT JOIN q7fbn_user_profiles ON q7fbn_users.id = q7fbn_user_profiles.userId");

当然,您需要将q7fbn_user_profiles.userId更改为您的字段名称,以便在该表中包含您的用户ID。

修改

基于配置文件的OP表结构:

$con = mysqli_connect("localhost", "blah", "blah", "blah");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Here is 2 Database
mysqli_query($con, "SET NAMES 'utf8'");
$sql = "SELECT * FROM q7fbn_users";
$result = mysqli_query($con, $sql);
echo "
<table border='1', table style='float:center' align='center'>
    <tr>
    <!--  Here is the database 1 fields -->
        <th>ID</th>
        <th>Name</th>
        <th>Username</th>
        <th>E-mail</th>
        <!-- Here is the phone number that i want lookup from database 1 -->
        <th>Phone</th>
    </tr>";
while ($row = mysqli_fetch_array($result)) {

    //Now you can use the $row['id'] what is the id of the user
    //so create another query to get the phonenumber.
    $sql = "SELECT profile_value FROM q7fbn_user_profiles"
        . " WHERE user_id = ". $row["id"] . ""
        . " AND profile_key = 'profile.phone'";
    $profileRes = mysql_query($con, $sql);
    //Get the phonenumber from the table2
    $phoneRow = mysqli_fetch_assoc($profileRes);
    //Initialize the phonenumber, maybe there are not phonnumber for this user.
    $phoneNumber = '';
    if ($phoneRow) {
        $phoneNumber = $phoneRow['profile_value'];
    }
    echo "<tr align='center'>";
    // Here is the database 1 fields
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    // Here is the phone number that i want lookup from database 1
    echo "<td>" . $phoneNumber . "</td>";
    echo "</tr>";
}
echo "</table>";