1

I am trying to make a while() loop, where I get some data from the database, and then I need to make another loop which is based on one of the variables given by the last while() loop.

There are no typo's as far I can see, but I keep getting

Fatal error: Call to a member function bind_param() on boolean in ... on line 77."

This is the code: (IMAGE FOR BETTER STRUCTURE: https://i.stack.imgur.com/wooxX.jpg)

    <?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 } ?> 

Any help is highly appreciated!

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • I've seen around here that some php MySQL libraries have difficulty handling multiple result sets simultaneously. Could that be it? Or is it that there is no param 'i' in your query? Aren't they usually of the form `:i`? – Uueerdo Sep 30 '15 at 00:17
  • It is possible your `prepare` failed or your connection failed making your `$stamt` equal `false` – Rasclatt Sep 30 '15 at 00:18
  • Try `print_r($connection);` and `print_r($stamt);` before `$stamt->bind_param('i', $uid);` Those *should* be objects that print out – Rasclatt Sep 30 '15 at 00:19
  • all the params and connections are proper, if i put the while loops seperately, they function just fine. (except for the $uid variable of course.) so i know they work, i am pretty sure it has something to do with that $uid variable, but if i echo it, it echos "10" just fine. – JakobPlenge Sep 30 '15 at 00:21
  • @Rasclatt , this is the output: what does that mean? mysqli Object ( [affected_rows] => -1 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 2014 [error] => Commands out of sync; you can't run this command now [error_list] => Array ( ) [field_count] => 4 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.24 [server_version] => 50624 [stat] => [sqlstate] => HY000 [protocol_version] => 10 [thread_id] => 12 [warning_count] => 0 ) – JakobPlenge Sep 30 '15 at 00:23
  • 4
    Looks like you are getting this error: http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now – Rasclatt Sep 30 '15 at 00:25
  • Then, your `$stamt` is not valid anymore (`false` I think) so it fails trying to run a query from a non-object. – Rasclatt Sep 30 '15 at 00:26
  • @Rasclatt I COULD KISS YOU :-* ! Thank you so much! i have been looking at this for hours, and now it is working! – JakobPlenge Sep 30 '15 at 00:28
  • No problemo! Glad it worked for you! Cheers – Rasclatt Sep 30 '15 at 00:29

1 Answers1

0

As written here: Commands out of sync; you can't run this command now

You can't have two simultaneous queries because mysqli uses unbuffered queries by default (for prepared statements; it's the opposite for vanilla mysql_query). You can either fetch the first one into an array and loop through that, or tell mysqli to buffer the queries (using $stmt->store_result()).

(Answered by @Rasclatt)

Community
  • 1
  • 1