1

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

Steven Tomko
  • 98
  • 11
  • I'm guessing the it is __on a non-object__ meaning `$picturestmt` isn't an object when you call `$picturestmt->bind_param('s', $postby);`. – Devon Bessemer Apr 28 '15 at 17:59
  • Change `$picturestmt->execute();` to `if(!$picturestmt->execute()){trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);}` to see if there's anything else MySQL wants to tell you. – Funk Forty Niner Apr 28 '15 at 18:00
  • @Fred -ii- - Nothing comes out because it stops at bind_param so it's not even getting executed. – Steven Tomko Apr 28 '15 at 18:53
  • @Devon - So what do you mean by that? – Steven Tomko Apr 28 '15 at 18:54
  • Do a var_dump on `$picturestmt` before you call bind_param. – Devon Bessemer Apr 28 '15 at 18:55
  • So now you know why the bind_param is failing. You're technically running `false->bind_param` which is not going to work. Now you need to debug why mysqli_prepare is returning false. – Devon Bessemer Apr 28 '15 at 19:30
  • @Devon - So I did error checking on mysqli->prepare and I got this "prepare() failed: Commands out of sync; you can't run this command now" But I don't know what does it mean :D – Steven Tomko Apr 28 '15 at 19:34

1 Answers1

0

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

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • So I did error checking on mysqli->prepare and I got this "prepare() failed: Commands out of sync; you can't run this command now" But I don't know what does it mean :D – Steven Tomko Apr 28 '15 at 19:36
  • It has to do with your bind_result. There are many stackoverflow questions already on this: http://stackoverflow.com/questions/19077779/php-mysqli-commands-out-of-sync-you-cant-run-this-command-now and http://stackoverflow.com/questions/15798762/mysqli-commands-out-of-sync-you-cant-run-this-command-now. – Devon Bessemer Apr 28 '15 at 19:38
  • OWW yeah I got it! I had to store result with $stmt->store_result(); and now it's working perfectly! Thanks for your help! – Steven Tomko Apr 28 '15 at 19:40
  • 1
    Glad you figured it out. Hopefully you know a little more about how to debug now. – Devon Bessemer Apr 28 '15 at 19:42