php不会回显内部联接查询的结果


php wont echo results from inner join query

我在网站上到处寻找类似的问题,有解决方案,但似乎没有解决我的问题。我有两张桌子;"友好者"answers"用户"。我正在尝试使用"friendlist"中的"FriendID"从"users"表中检索信息。一切都很好,直到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查询,它运行得非常好。我不知道怎么了。任何帮助都将不胜感激。感谢

您的代码是正确的,但您只错过了$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>";
    }

问题出在您的SQL查询上。你没有把友谊赛。所选部件上的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."'";