2012-01-13 70 views
0

有人能告訴我爲什麼下面的代碼只能得到除查詢以外的查詢中的所有圖片嗎?查詢沒有得到所有mysql_fetch_assocs的

$userquery = mysql_query("SELECT * FROM acceptedfriends WHERE profilename='$profilename' ORDER BY RAND() LIMIT 4"); 
    while ($userrun = mysql_fetch_assoc($userquery)) 
    { 
     $users = $userrun['username']; 
     $imagequery = mysql_query("SELECT * FROM users2 WHERE username='$users'"); 
     while ($imagefetch = mysql_fetch_assoc($imagequery)) 
     { 
      $location = $imagefetch['imagelocation']; 
      $image = "<img src='$location' width='60' height='40'>"; 
      if ($profilename==$username) 
      { 
       echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr> <td>'.$image.'</td><td><div style="margin-bottom:5px;"><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></div><div><a href="http://www.pearlsquirrel.com/conversation.php/'.$users.'" style="text-decoration:underline;" target="_blank"><div style="font-size:.7em";>Click to enter a conversation.</div></a></div></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
      } 
      else 
      { 
       echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr><td>'.$image.'</td><td><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
      } 
     } 
    } 

$image = "<img src='$location' width='60' height='40'>"; 

沒有查詢獲​​得最後一張圖像。我花了大約一小時試圖解決這個問題,不知道。任何幫助,將不勝感激。

具有相同的錯誤

$userquery = mysql_query("SELECT * FROM acceptedfriends WHERE  profilename='$profilename' ORDER BY id DESC LIMIT 6"); 
    while ($userrun = mysql_fetch_assoc($userquery)) 
    { 
     $users = $userrun['username']; 
     $location = $userrun['imagelocation']; 
     $image = "<img src='$location' style='width:60px; height:40px;'>"; 
     if ($profilename==$username) 
     { 
      echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr><td>'.$image.'</td><td><div style="margin-bottom:5px;"><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></div><div><a href="http://www.pearlsquirrel.com/conversation.php/'.$pageusers.'" style="text-decoration:underline;" target="_blank"><div style="font-size:.7em";>Click to enter a conversation.</div></a></div></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
     } 
     else 
     { 
      echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr><td>'.$image.'</td><td><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
     } 
    } 
+0

你沒有得到最後的圖像嗎?使用mysql_num_rows()來獲得返回的行數,從那裏我們可以進一步調查。 – Chibuzo 2012-01-13 02:29:21

+0

所以你只能得到3張圖片?並通過','圖像=「」;'真的應該''圖像=「」;':) – davogotland 2012-01-13 02:37:34

回答

3

當您運行的print_r(mysql_fetch_assoc($ userquery))會發生什麼簡化代碼;在兩個選擇語句?你看到數組中的數據嗎?我假設你故意做極限4?通常我不跑的另一內部while循環,可以改爲嘗試這個辦法:

$userquery = mysql_query("SELECT * FROM acceptedfriends WHERE profilename='$profilename' ORDER BY RAND() LIMIT 4"); 

while ($userrun = mysql_fetch_assoc($userquery)) { 
    $userArray[] = $userrun; 
} 

print_r($userArray); 
echo '<br /><br />'; 

foreach ($userArray as $userValue) { 
    $users = $userValue['username']; 
    $imagequery = mysql_query('SELECT * FROM users2 WHERE username="'.$users.'"'); 
    while ($imagefetch = mysql_fetch_assoc($imagequery)) { 
     //echo out variables from the above select to make sure you're getting them 
     $location = $imagefetch['imagelocation']; 
     $image = "<img src='$location' width='60' height='40' />"; 
     if ($profilename==$username) { 
      echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr> <td>'.$image.'</td><td><div style="margin-bottom:5px;"><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></div><div><a href="http://www.pearlsquirrel.com/conversation.php/'.$users.'" style="text-decoration:underline;" target="_blank"><div style="font-size:.7em";>Click to enter a conversation.</div></a></div></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
     } else { 
      echo '<div id="hovercolor2" style="width:294px; float:left;"><table><tr><td>'.$image.'</td><td><a href="http://www.pearlsquirrel.com/'.$users.'" target="_blank">'.$users.'</a></td></tr></table></div><div id="hrdiv3" style="float:left; width:298px;"></div>'; 
     } 
    } 
} 

確保通過檢查查詢腳本時存在的值來進行調試。希望這會有所幫助

+0

我也不會運行嵌套循環,它只是要求麻煩。 – Neilos 2012-01-13 02:53:52

+0

是嵌套循環總是一個壞主意,你的問題可能是這樣的:http://www.php.net/manual/en/control-structures.while.php#52733 – 2012-01-13 03:04:18

+0

我考慮到你們在說什麼,我簡化了我的代碼,但我仍然有同樣的問題。你介意再看一遍嗎?我會把它放在頂部。 – Eggo 2012-01-13 03:43:01

0

錯誤不在於那段代碼,而是在頁面上的一個較早的div,我忘了結束'。