2012-01-13 62 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,我忘了结束'。