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

时间:2013-11-27 09:04:45

标签: php mysql inner-join

我已经在网站上查找过类似问题的解决方案,而非似乎解决了我遇到的问题。我有两张桌子; “friendlist”和“users”。我正在尝试使用“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查询,它运行得很好。我不知道出了什么问题。任何帮助都感激不尽。感谢

2 个答案:

答案 0 :(得分:0)

问题在于您的SQL查询。 你没有把friendlist.UserId放在select部分。 这就是为什么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."'";

答案 1 :(得分:0)

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