Php 如何使用另一个表从表中查找值
我对从表中查找数据有疑问,。。。 有一个表可以保存用户的4个字段:id、名称、用户名和电子邮件 我还有另外一个表,它可以从用户那里保存10个非必填字段:phone和 现在我想从表2中的表1中查找Id,并在表中显示电话号码。 注:ID是表1和表2之间的共享代码Php 如何使用另一个表从表中查找值,php,html,mysql,Php,Html,Mysql,我对从表中查找数据有疑问,。。。 有一个表可以保存用户的4个字段:id、名称、用户名和电子邮件 我还有另外一个表,它可以从用户那里保存10个非必填字段:phone和 现在我想从表2中的表1中查找Id,并在表中显示电话号码。 注:ID是表1和表2之间的共享代码 <?php $con=mysqli_connect("localhost","blah","blah","blah"); if (mysqli_connect_errno()) { echo "Failed to connec
<?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);
?>
您的问题是,在您的行中只有来自用户表的数据,因为您只从$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更改为您字段的名称,表中的用户标识包含哪些内容
编辑:
根据配置文件的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>";
需要更多关于如何在查询中连接2个表的详细信息,我为我的php编写了代码,但表中没有显示任何内容…请向我显示用户配置文件表的结构。请查看我编辑的代码和我的注释,并请试着理解我所做的操作。您知道为什么配置文件中的某些值是这样的:\u062f\u0627\u0646\u0634\?它是UTF8,但值不是!我认为这些是json编码的数组或UTF编码的字符串。