2013-11-27 45 views
1

我环顾网站寻找解决方案的类似问题,而且似乎无法解决我遇到的问题。我有两张桌子; “好友列表”和“用户”。我试图使用“friendlist”中的“FriendID”从“users”表中检索信息。一切工作正常,直到while($row = mysqli_fetch_array($result)){}循环,然后没有其他打印。php不会回应来自内部连接查询的结果

我的代码如下:

$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查询,它工作得很好。我不知道什么是错的。任何帮助都感激不尽。谢谢

+0

echo mysqli_num_rows($ result);并检查它们有多少行? –

+2

您很可能倾向于SQL注入,并且需要尽快修复此问题,除非您希望任何人都能够显示整个系统中的所有用户。使用预准备的语句和绑定变量。神奇地将'mysql_'改为'mysqli_'并不能解决这个问题。 – h2ooooooo

+0

或者先在phpmyadmin中运行你的查询来检查它们是否有内容。 –

回答

0

问题出在你的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."'"; 
+0

我试过,但它没有工作:(我不认为这是有道理的选择friendlist.UserId如果我想使用它作为搜索条件......如果你知道我的意思 – user1726443

+1

我才意识到我什么错误是,如此愚蠢!> __ <$ ID没有设定值然而,在脚本的开始,当我把我的会话。 '在session_start(); $ ID = $ _SESSION [“身份证”] ;' 我以为我已经分配的$ id与价值,我不得不为它分配再次'$ ID = $ _SESSION'(不知道为什么寿),现在它的工作原理 感谢您的建议,并深表歉意。 ... PS我是否需要删除问题,因为代码不是问题? – user1726443

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