PHP查询以检索朋友(朋友)信息并列出缩略图,其中包含指向朋友信息的链接

时间:2011-02-03 10:17:58

标签: php mysql

我正在创建一个社交网站,我希望在PHP生成的表中显示用户的朋友。我想在这些缩略图下方显示朋友缩略图和其他信息,这样如果您点击缩略图,就会转到该用户的个人资料。

我的用户表包含字段,例如

user_id, username, user_first_name, user_last_name...etc

我有一个好友表:( user1_id,user2_id是用户表中PK user_id的FK)

pal_id  user1_id      user2_id       status     timestamp
6              92            95         1        2011-02-02 21:44:58
7               98            97         0        2011-02-02 21:44:28
8              92            98         0        2011-02-02 21:44:28

图片表,其中检索了头像以供显示。如果avatar ='1',那就是用户头像:

picture_id  picture_url                picture_thumb_url                 user_id    avatar  timestamp
73  ../User_Images/66983.jpg    ../User_Images/Thumbs/66983.jpg          92           1     2011-02-01 21:41:59
74  ../User_Images/56012.jpg    ../User_Images/Thumbs/56012.jpg          94           0     2011-01-25 12:09:58

从上面来看,用户92是用户95的朋友,因为他们已经确认了友谊(显示为status ='1')

朋友中的

user1_id获取友情发起者的用户ID。对于pal_id = 6,用户92请求友谊,95确认它。如果用户95请求了友谊并且已经确认,则user2_id将会读取92 ...

现在,我将如何显示我的PHP查询以显示朋友缩略图和信息?

我的PHP代码:

<?php require_once('Connections/connections.php'); ?>
<?php
//query username
$user_id = $_SESSION['UserSession'];
$user_id = mysql_real_escape_string($user_id);
$query_user_info = "SELECT username FROM users WHERE user_id='$user_id'";
$user_info = mysql_query($query_user_info, $connections) or die(mysql_error());
$row_user_info = mysql_fetch_assoc($user_info);

//code for displaying all your pals
$query_pal_no = "SELECT * FROM pals WHERE (user1_id='$user_id' AND status='1') OR (user2_id='$user_id' AND status='1')";
$pal_no_result = mysql_query($query_pal_no, $connections) or die(mysql_error());
$row_pal_no = mysql_num_rows($pal_no_result);

while ($pal_no = mysql_fetch_assoc($pal_no_result))
{
    //get pal info
    $query_pal_info = "SELECT users.user_id, user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users LEFT JOIN picture ON
    users.user_id =  picture.user_id
    AND picture.avatar=1 WHERE users.user_id = {$pal_no['user2_id']}";
    $pal_info = mysql_query($query_pal_info , $connections) or die(mysql_error());
    $totalRows_pal_info = mysql_num_rows($pal_info );

    //echo table with pal information
    echo "\n<table>";
    $j = 5;
    while ($row_pal_info = mysql_fetch_assoc($pal_info))
    {
        if($j==5) echo "\n\t<tr>";
        $thumbnail_user = $row_pal_info['picture_thumb_url'] != '' ? $row_pal_info['picture_thumb_url'] : '../Style/Images/default_avatar.png';
        echo "<td width='100' height='100' align='center' valign='middle'><a href = 'user_view.php?user_id2={$row_pal_info['user_id']}'>
        <img src='/NNL/User_Images/$thumbnail_user' border='0'/></a></td>\n";
        $j--;
        if($j==0) {
        echo "\n\t</tr>\n\t<tr>";
        $j = 5;
        } 
    }
    if($j!=5) echo "\n\t\t<td colspan=\"$j\"></td>\n\t</tr>";
    echo "\n</table>";
}
?>
<table width="500" border="0">
    <tr>
    <td height="20"><div class="heading_text_18"><?php echo $row_user_info ['username']; ?>'s&nbsp;pals <?php echo $row_pal_no ?></div>  </td>
    </tr>
    <tr>
      <td class="interactionLinksDiv" align="right" style="border:none;"><a href="#" onclick="return false" 
      onmousedown="javascript: toggleInteractContainers('pal_requests');">Pal Requests</a></td>
    </tr>
    <tr>
      <td height="5"></td>
    </tr>
</table>

我知道问题出在查询中的WHERE之后:

$query_pal_info = "SELECT users.user_id, user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users LEFT JOIN picture ON
    users.user_id =  picture.user_id
    AND picture.avatar=1 WHERE users.user_id = {$pal_no['user2_id']}";

正如我目前所知,它显示用户92唯一的朋友是用户95.这只能起作用,因为92是发起者,95是接受者。如果95是发起者,则查询将不起作用,因为查询会将用户92(他自己)作为他的朋友返回。

什么是正确的查询?感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:0)

SELECT * FROM users u
INNER JOIN pal p
ON p.user1_id = u.user_id
INNER JOIN pictures pi
ON pi.user_id = p.user2_id
WHERE u.user_id = 92

这应该为您提供用户ID为92的所有palsand缩略图。