[英]php wont echo results from inner join query
我到处寻找解决方案的类似问题,但似乎无法解决我遇到的问题。 我有两个桌子。 “朋友列表”和“用户”。 我正在尝试使用“朋友列表”中的“ FriendID”来从“用户”表中检索信息。 一切正常,直到while($row = mysqli_fetch_array($result)){}
循环,然后再无其他显示。
我的代码如下:
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";
$result = mysqli_query($con, $query);
if(!$result){
echo "<br/><h4>You currently do not have any friends. Please click the Find Friends button to find a friend</h4>";
}else{
echo "<center><br/>Here is a list of all your friends:<br/>";
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>Pro Pic: <img style='width:200px; height:200px' alt='No Profile Picture' src='uploads/" .$row['Picture']. "' /></td>";
echo "<td>Name :" .$row['Name']. "</td>";
echo "<td>Surname :" .$row['Surname']. "</td>";
echo "<td><form method='post' action='viewFriend.php'>";
echo "<input type='hidden' name='friendId' value='".$row['FriendID']."'/>";
echo "<input type='submit' name='View' value='View Profile'/>";
echo "</form></td>";
echo "</tr>";
}
echo "</table></center>";
}
浏览器上未显示任何内容。 仅显示第4级标题文本:“这里是所有朋友的列表”。 但是之后那是空的空间。
我已经检查了mySql上的sql查询,它工作得很好。 我不知道怎么了 任何帮助都感激不尽。 谢谢
问题出在您的SQL查询上。 您没有在选择部分上放置friendlist.UserId。 这就是为什么where子句看不到在哪里进行比较的原因。
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";
您的代码是正确的,但是您仅错过了$ id变量的声明。 如下使用。
//initialize the $id as.
$id = $_REQUEST['id'];
$query = "SELECT friendlist.FriendID, users.Name, users.Surname, users.Picture
FROM friendlist
INNER JOIN users
ON friendlist.FriendID = users.Id
WHERE friendlist.UserId ='".$id."'";
$result = mysqli_query($con, $query);
if(!$result){
echo "<br/><h4>You currently do not have any friends. Please click the Find Friends button to find a friend</h4>";
}else{
echo "<center><br/>Here is a list of all your friends:<br/>";
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>Pro Pic: <img style='width:200px; height:200px' alt='No Profile Picture' src='uploads/" .$row['Picture']. "' /></td>";
echo "<td>Name :" .$row['Name']. "</td>";
echo "<td>Surname :" .$row['Surname']. "</td>";
echo "<td><form method='post' action='viewFriend.php'>";
echo "<input type='hidden' name='friendId' value='".$row['FriendID']."'/>";
echo "<input type='submit' name='View' value='View Profile'/>";
echo "</form></td>";
echo "</tr>";
}
echo "</table></center>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.