dongsuoxi1790 2015-04-28 17:47
浏览 99
已采纳

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

I want to bind the result from prepared query which is in while stmt. But it gives me the error

Call to a member function bind_param

We all know this obvious error message... I just don't know why is this error popping out...

Here is my php code (session is started and all rows and columns are correct)

$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'>";
                                }
                        }
}

This code should assign the $profpicturefromdb to an image. (And also pull out all posts from database but that's another echo)

If I echo the $postby, it shows the name of user who posted the post. That's fine, but why it does not assign that to the "bind_param('s',$postby'); ? Thanks

  • 写回答

1条回答 默认 最新

  • drhwb470572 2015-04-28 19:35
    关注

    The full error is:

    Call to a member function bind_param on a non-object

    This means $picturestmt isn't an object when you call:

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

    As you could see from var_dump($picturestmt): $picturestmt is false.

    So we can see, technically, false->bind_param is generating that error.

    You have to turn to debugging why mysqli_prepare is returning false:

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

    This can be done using mysqli::error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?