簡體   English   中英

遍歷mysqli結果

[英]Looping through a mysqli result

我正在嘗試顯示登錄用戶正在關注的藝術家的狀態更新列表。

到目前為止,我有這個:

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

}

但是我不確定如何遍歷並顯示返回的狀態更新?

這不是我的強項,因此任何指針都將不勝感激!

是什么使您無法執行與第一次查詢類似的操作? 如下所示:

#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 ,那么您可以僅使用一個查詢進行artist_id (但不知道您是否要這樣做)。

只是為了避免多個查詢,您可以使用一個查詢,如下所示:

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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM