简体   繁体   English

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

[英]php wont echo results from inner join query

I've looked around the site for similar problems with solutions and non seem to fix the problem I have. 我到处寻找解决方案的类似问题,但似乎无法解决我遇到的问题。 I have two tables; 我有两个桌子。 "friendlist" and "users". “朋友列表”和“用户”。 I'm trying to use the "FriendID" from "friendlist" to retrieve information from the "users" table. 我正在尝试使用“朋友列表”中的“ FriendID”来从“用户”表中检索信息。 Everything works fine up until the while($row = mysqli_fetch_array($result)){} loop then nothing else prints. 一切正常,直到while($row = mysqli_fetch_array($result)){}循环,然后再无其他显示。

My code is as follows: 我的代码如下:

$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>";
}

Nothing is displayed on the browser. 浏览器上未显示任何内容。 Only the level 4 heading text: "Here is a list of all your friends" shows. 仅显示第4级标题文本:“这里是所有朋友的列表”。 But after that its empty space. 但是之后那是空的空间。
I've checked the sql query on mySql and it works perfectly fine. 我已经检查了mySql上的sql查询,它工作得很好。 I have no idea what's wrong. 我不知道怎么了 Any help will be much appreciated. 任何帮助都感激不尽。 Thanks 谢谢

The problem is on your SQL Query. 问题出在您的SQL查询上。 You did not put friendlist.UserId on the select part. 您没有在选择部分上放置friendlist.UserId。 That's why the where clause do not see where to compare it. 这就是为什么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."'";

Your code is correct but you miss the declaration of $id variable only. 您的代码是正确的,但是您仅错过了$ id变量的声明。 Use like below. 如下使用。

//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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM