【问题标题】:How to bind paramater from binded result in while stmt如何在while stmt中从绑定结果绑定参数
【发布时间】:2015-07-07 17:14:22
【问题描述】:

我想绑定 while stmt 中准备好的查询的结果。但它给了我错误

调用成员函数bind_param

我们都知道这个明显的错误信息...我只是不知道为什么会弹出这个错误...

这是我的 php 代码(会话已启动,所有行和列都正确)

$selectposts = "SELECT postby,posttxt,time,upvotes,downvotes FROM posts ORDER BY id DESC";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime,$upvotes,$downvotes);
while($stmt->fetch()){
   $picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
        $picturestmt->bind_param('s', $postby);
        $picturestmt->execute();
        $picturestmt->bind_result($picture);
                        while($picturestmt->fetch()){
                                if(empty($picture)){
                                        $profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
                                } else {
                                        $profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
                                }
                        }
}

此代码应将 $profpicturefromdb 分配给图像。 (并且还从数据库中提取所有帖子,但这是另一个回声)

如果我回显 $postby,它会显示发布帖子的用户名。很好,但为什么它不将其分配给 "bind_param('s',$postby'); ? 谢谢

【问题讨论】:

  • 我猜它是在一个非对象上,这意味着当你调用$picturestmt-&gt;bind_param('s', $postby);$picturestmt 不是一个对象。
  • $picturestmt-&gt;execute(); 更改为 if(!$picturestmt-&gt;execute()){trigger_error("there was an error....".$mysqli-&gt;error, E_USER_WARNING);} 看看 MySQL 是否还有其他要告诉您的信息。
  • @Fred -ii- - 什么也没有出现,因为它停在 bind_param 所以它甚至没有被执行。
  • @Devon - 那你是什么意思?
  • 在调用 bind_param 之前对 $picturestmt 执行 var_dump。

标签: php mysqli


【解决方案1】:

完整的错误是:

在非对象上调用成员函数 bind_param

这意味着 $picturestmt 在您调用时不是对象:

$picturestmt->bind_param('s', $postby);

var_dump($picturestmt) 可以看出:$picturestmtfalse

所以我们可以看到,从技术上讲,false-&gt;bind_param 正在生成该错误。

你必须转向调试为什么 mysqli_prepare 返回 false:

$picturestmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");

这可以使用 mysqli::error

来完成

【讨论】:

  • 所以我对 mysqli->prepare 进行了错误检查,我得到了这个“prepare() failed: Commands out of sync; you can't run this command now” 但我不知道是什么这意味着:D
  • 这与您的 bind_result 有关。已经有很多关于这个的stackoverflow问题:stackoverflow.com/questions/19077779/…stackoverflow.com/questions/15798762/…
  • 哇,我明白了!我不得不用 $stmt->store_result(); 存储结果现在它运行良好!感谢您的帮助!
  • 很高兴你知道了。希望您现在对如何调试有更多了解。
猜你喜欢
  • 1970-01-01
  • 2013-11-17
  • 2014-09-05
  • 2016-12-08
  • 2017-08-17
  • 2023-03-24
  • 2016-01-18
  • 2015-11-25
  • 1970-01-01
相关资源
最近更新 更多