2013-07-31 66 views
0

我试图显示登录用户所关注的艺术家的状态更新列表。通过mysqli结果循环

到目前为止,我有这样的:

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

} 

,但我不知道如何来遍历并显示返回的状态更新?

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

+1

使用内循环的同时,像你遍历第一个查询的结果。 – Achrome

回答

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' 
+1

感谢你 - 这似乎是一个更简单的方法。 –

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_likesstatus_updates有共同artist_id那么你可以只使用一个连接一个查询。 (但不知道你是否在要求)。