查询没有获得所有mysql_fetch_assocs

时间:2012-01-13 02:22:14

标签: php

有人可以告诉我为什么下面的代码只能获取查询中的所有图像,除了最后一个?

    $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>';
        }
    }

2 个答案:

答案 0 :(得分:3)

运行print_r时会发生什么(mysql_fetch_assoc($ userquery));在两个选择语句?你看到数组中的数据了吗?我假设你故意做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>';
        }
    }
}

确保在查询脚本时检查您的值是否存在进行调试。希望这有帮助

答案 1 :(得分:0)

错误不在于那段代码,而是在页面上有一个早先的div,我忘了结束'。