简体   繁体   English

遍历mysqli结果

[英]Looping through a mysqli result

I'm trying to display a list of status updates from artists that a logged in user is following. 我正在尝试显示登录用户正在关注的艺术家的状态更新列表。

So far I have this: 到目前为止,我有这个:

#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

  #Now grab the statuses for each artist
  $status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
  $status_result = mysqli_query($dbc,$status_query)

}

But i'm not sure how to loop through and display the returned status updates? 但是我不确定如何遍历并显示返回的状态更新?

This isn't a strong point of mine, so any pointers would be greatly appreciated! 这不是我的强项,因此任何指针都将不胜感激!

What prevented you from doing similar to what you'd already done for the first query? 是什么使您无法执行与第一次查询类似的操作? Something like follows: 如下所示:

#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

  #Now grab the statuses for each artist
  $status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
  $status_result = mysqli_query($dbc,$status_query)

  while($status_result_row = mysqli_fetch_assoc($status_result)) {
    echo $status_result_row['mycol']; // This is where you know better than us
  }
}

Or if those two tables artist_likes and status_updates have artist_id in common then you could just use one query with a join. 或者,如果这两个表artist_likesstatus_updates具有artist_id ,那么您可以仅使用一个查询进行artist_id (But don't know if you are asking for that). (但不知道您是否要这样做)。

Just for avoiding multiple query, you can use one query like this: 只是为了避免多个查询,您可以使用一个查询,如下所示:

SELECT l.*, s.* 
from artist_likes l, status_updates s
WHERE
l.artist_id = s.artist_id and
l.user_id = '1'

or 要么

SELECT l.*, s.* 
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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