如何在php中使用sql查询循环?

时间:2018-05-16 21:57:45

标签: php sql

我需要配置用户的Feed,以显示第一位用户关注的用户发布的帖子。 喜欢Instagram feed。我有一个posts表。有author_id列。在API请求中,我们有requester_id POST参数。 我们有第二个名为relations的表(以下是关注者)。 只有两列 - follow_byfollow_to。 使用PHP的SQL请求后,如何通过用户requester获取帖子。 这意味着relations必须是follow_to = author_id(from post)follow_by = requester_id的行。 对不起有些错误(英文)。 感谢。

1 个答案:

答案 0 :(得分:0)

我认为你应该执行子查询:

<?php
if($sq = $mysql_connection->query('SELECT `follow_by` following, `follow_to` followers, `author_id` id FROM `relations` WHERE `author_id`=(SELECT `author_id` FROM `posts`)')){
  if($sq->num_rows){
    while($row = $sq->fetch_object()){
      // each row now has the following attributes within the loop
      // $row->following // $row->followers // $row->id
    }
  }
  $sq->free();
}
$mysql_connection->close();
?>

当然,如果您使用客户端信息来处理MySQL查询,那么您将要准备好您的语句。