如何在 stmt 中从绑定的结果绑定参数



我想绑定来自 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');?谢谢

完整的错误是:

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

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

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

正如你从var_dump($picturestmt)中看到的:$picturestmt的。

因此,从技术上讲,我们可以看到false->bind_param正在产生该错误。

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

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

这可以使用mysqli::error来完成

最新更新