【问题标题】:Trying to get a while loop inside another while loop, but it is not working together [duplicate]试图在另一个while循环中获得一个while循环,但它不能一起工作[重复]
【发布时间】:2015-12-27 14:56:22
【问题描述】:

我正在尝试创建一个while() 循环,从数据库中获取一些数据,然后我需要创建另一个循环,该循环基于最后一个while() 循环给出的变量之一。

据我所见,没有错字,但我不断收到

致命错误:在第 77 行的布尔值中调用成员函数 bind_param()。”

这是代码:(结构更好的图片:http://imgur.com/0dFhoRb

    <?php                

$sql = 'SELECT raised_id, user_id, project_id, amount FROM cf_donations ORDER BY raised_id DESC LIMIT 6';
$stmt = $connection->prepare($sql);
$stmt->bind_result($rid, $uid, $pid, $amount);
$stmt->execute();

while($stmt->fetch()){ ?>


    <?php
            $userinfoquery = 'SELECT first_name, last_name, profile_image FROM cf_users WHERE user_id = ?';
            $stamt = $connection->prepare($userinfoquery);   
            $stamt->bind_param('i', $uid);
            $stamt->bind_result($firstname, $lastname, $image);
            $stamt->execute(); 

            while($stamt->fetch()){ ?> 

                <div class="donationDiv">
                    <p><?= $amount ?>$</p>
                </div>            


            <?php } ?>                                                                                                              
<?php } ?> 

非常感谢任何帮助!

【问题讨论】:

  • 我在这里看到一些 php MySQL 库难以同时处理多个结果集。会是这样吗?还是您的查询中没有参数“i”?它们通常不是:i 的形式吗?
  • 您的prepare 可能失败或您的连接失败使您的$stamt 等于false
  • $stamt-&gt;bind_param('i', $uid); 之前尝试print_r($connection);print_r($stamt); 那些应该是打印出来的对象
  • 所有参数和连接都是正确的,如果我将while循环分开放置,它们就可以正常工作。 (当然除了 $uid 变量。)所以我知道它们可以工作,我很确定它与 $uid 变量有关,但如果我回显它,它会回显“10”就好了。
  • 看起来您遇到了这个错误:stackoverflow.com/questions/614671/…

标签: php mysql database while-loop


【解决方案1】:

写在这里:Commands out of sync; you can't run this command now

您不能同时进行两个查询,因为 mysqli 默认使用无缓冲查询(对于准备好的语句;对于普通的 mysql_query 则相反)。您可以将第一个获取到数组并循环遍历,或者告诉mysqli 缓冲查询(使用$stmt-&gt;store_result())。

(由@Rasclatt 回答)

【讨论】:

    猜你喜欢
    • 2016-01-11
    • 2022-10-12
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多