使用mysql_query的嵌套循环会产生无限循环

时间:2013-10-12 08:31:24

标签: php mysql loops while-loop

对不起标题,但我不能更具体(英语不是我的母语)。这是我的问题:我有2个while循环使用mysql_query()作为表达式(我知道它已被弃用但我必须使用它)。虽然第一个循环很好,但第二个循环是不定式循环,我不知道为什么。我在phpMyAdmin中测试了mysql查询,没关系。这是我的代码:

while($czat = mysql_fetch_array($data))
{
$user = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id` = \"{$czat['user_id']}\""));


$feed = $feed.'

<div class="container dropshadow">
<div class="sidebar">
    <img class="avatar" src="../'.$user['avatar'].'" />
</div>
<div class="container2">
        <div class="profile">
            <div class="username">'.$user['user'].'</div>
            <div class="datetime">'.$czat['timestamp'].'</div>
        </div>
        <div class="content">
            '.$czat['message'].'
        </div>
        <div class="answer">odpowiedz<div style="display:none;">'.$czat['id'].'</div></div>
</div>
</div>

<br />';

while($row = mysql_fetch_array(mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20")))
{
    $reply_user = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id` = \"{$row['user_id']}\""));
    $feed = $feed . '
        <div class="odpowiedzi">
            <div class="sidebar3"><img src="../'.$reply_user['avatar'].'" /></div>
            <div class="container3">
                <div class="info">'.$reply_user['user'].'</div>
                <div class="timestamp">'.$row['timestamp'].'</div>
            </div>
            <div class="reply">'.$row['message'].'</div>
        </div>
    ';
}

$feed = $feed . '
<div class="form_odpowiedz" id="'.$czat['id'].'">   
    <img src="../'.$user['avatar'].'" class="avatar2" />
    <form action="" name="odpowiedz"">
        <input type="textbox" name="text" class="text_odpowiedz" id="msg_reply_'.$czat['id'].'" />
        <input type="hidden" name="answer_id" value="'.$czat['id'].'" class="msg_id" />
        <input type="submit" value="Odpowiedz" style="heigth: 30px;" class="odpowiedz" alt="'.$czat['id'].'" />
    </form>
    <br />
</div>


';}

我希望你能帮助我。

4 个答案:

答案 0 :(得分:2)

从第二个while循环中删除查询语句。在你的陈述中

while($row = mysql_fetch_array(mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20")))

你正在做的是每次在while循环中查询相同的语句。这导致了无限循环 做到这样: -

$res = mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20")
while($row = mysql_fetch_array($res))
{
 //you stuff goes here
 }

这样可以正常工作。测试一下。

答案 1 :(得分:1)

此:

mysql_fetch_array(mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20"))

一遍又一遍地执行。您一遍又一遍地触发相同的查询,并反复获取其第一个结果。您需要运行查询一次,然后只在循环中从结果中获取。

答案 2 :(得分:1)

更改

while($row = mysql_fetch_array(mysql_query("SELECT * FROM {czat2 {1}} {回复{1}} ID为WHERE

要,

= \"{$czat['id']}\" ORDER BY

答案 3 :(得分:1)

我认为这是因为你在每次循环迭代时重新执行查询。

而不是:

while($row = mysql_fetch_array(mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20")))
{

待办事项

$result = mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20");
while($row = mysql_fetch_array($result)) {