0

我正在尝试显示登录用户正在关注的艺术家的状态更新列表。

到目前为止,我有这个:

#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)

}

但我不确定如何循环并显示返回的状态更新?

这不是我的强项,所以任何指针将不胜感激!

4

2 回答 2

3

是什么阻止了你做类似于你已经为第一个查询所做的事情?如下所示:

#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
  }
}

或者,如果这两个表artist_likes有共同点status_updatesartist_id那么您可以只使用一个带有连接的查询。(但不知道您是否要求这样做)。

于 2013-07-31T23:25:33.700 回答
0

只是为了避免多个查询,您可以使用这样的一个查询:

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

或者

SELECT l.*, s.* 
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'
于 2013-07-31T23:27:02.100 回答