【问题标题】:How to display a feed using PHP and MySqli如何使用 PHP 和 MySqli 显示提要
【发布时间】:2015-04-24 03:12:07
【问题描述】:

我有一个名为 users_friends 的表。此表中有 4 列,user_oneuser_twostatus 和 created(Date Created)。

如果状态设置为“2”,则两个用户都是朋友。否则,他们处于待处理的好友请求中。

我想通过从我的表 posts 中获取帖子来显示提要页面,其中 user_id 等于 id(current_user),然后是 user_friends 表。 mysql语句会是什么?我被困住了。

这就是我获取用户朋友的方式。

$conn = conn();
$sql = ("SELECT * FROM users_friends WHERE status='2' AND (friend_one='$id' OR friend_two='$id')");
$result = $conn->query($sql);
     if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
               $fndOne = $row["friend_one"];
               $fndTwo = $row["friend_two"];
               if ($fndOne == $id) {
                    $fndIs = $fndTwo;
               } elseif ($fndTwo == $id) {
                    $fndIs = $fndOne;
               }
               //Just shows it works
               echo "$fndIs<br />";
          }
     }

有没有更好的方法让用户成为朋友?因为我似乎无法弄清楚如何获取用户的朋友和当前用户,然后显示所有按 post_id DESC 排序的帖子。

【问题讨论】:

  • friend_one 基本上是当前用户,friend_two 是他们的朋友?

标签: php mysql mysqli server


【解决方案1】:
$qry="SELECT user_one,user_two FROM user_friends WHERE (user_one='$id' || user_two='$id') AND status=2"; 
$res=mysqli_query($qry);
while($res_fetch=mysqli_fetch_object($res))
{



$qry_p="SELECT * FROM post WHERE (user_id='$res_fetch->user_one' || user_two='$res_fetch->user_one' || user_id='$res_fetch->user_two' || user_two='$res_fetch->user_two')"
$res_p=mysqli_query($qry_p);
while($res_fecth_p=mysqli_fetch_object($res_p))

{
/* Your Post*/

}

}

【讨论】:

    【解决方案2】:

    难道你不能在你的 SQL 语句中使用内部联接并加入 users_freinds.user_id = posts.user_id 并以这种方式获取数据吗?

    还是我误解了你的要求?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多