2012-01-13 11 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'>"; 

は、クエリの最後の画像を取得されていません。私はこれを解決しようとしている時を過ごす時間を費やしています。どんな助けもありがとう。あなたがしますprint_r(mysql_fetch_assoc($のuserquery))を実行するとどうなりますか同じエラー

$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

+0

あなたは3枚の画像しか得ていませんか? '$ image =" ";'本当に '$ image =" ";' :) – davogotland

答えて

3

でコードを簡素化

。両方のselect文で?配列内のデータが見えますか?私は、あなたがLIMIT 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

+0

はいネストされたループは常に悪い考えです。あなたの問題はおそらくこれでしょう:http://www.php.net/manual/en/control-structures.while.php#52733 –

+0

私は皆さんが言っていたことを考慮に入れました。私はコードを少し簡略化しましたが、私はまだ同じ問題を抱えています。もう一度それを見てもらえますか?私は一番上に投稿します。 – Eggo

0

エラーはそのコードではなく、前のdivで、私が 'を終了するのを忘れたページにあります。

関連する問題