-2

抱歉标题,但我不能更具体(英语不是我的母语)。这是我的问题:我有 2 个使用 mysql_query() 作为表达式的 while 循环(我知道它已被弃用,但我必须使用它)。虽然第一个循环很好,但第二个是不定式循环,我不知道为什么。我在 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

4 回答 4

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
 }

这将正常工作。测试一下。

于 2013-10-12T08:41:34.900 回答
1

这个:

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

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

于 2013-10-12T08:37:38.423 回答
1

改变

while($row = mysql_fetch_array(mysql_query("SELECT * FROMczat2WHERE回复= \"{$czat['id']}\" ORDER BYidDESC LIMIT 20")))

至,

$res = mysql_query("SELECT * FROM `czat2` WHERE `reply` = \"{$czat['id']}\" ORDER BY `id` DESC LIMIT 20");
while($row = mysql_fetch_array($res)) { /* your logic here */}
于 2013-10-12T08:38:25.500 回答
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)) {
于 2013-10-12T08:38:46.217 回答