0

I have the following function which has both for and while loop to execute a prepared statement. But it is throwing error as Commands out of sync; you can't run this command now

Here is my function

public function queryProductDetail() {

    $product_info = $this->mysqliengine->prepare("select product_id, sku from product limit 10");
    $product_info->execute();
    $product_info->bind_result($product_id,$sku);

    $product_image = $this->mysqliengine->prepare("insert into product_image(product_id,thumb_image,medium_image,original_image) values(?,?,?,?)") or die($this->mysqliengine->error);
    $product_image->bind_param('isss',$api_product_id, $thumb_image, $medium_image, $original_image);


    while($product_info->fetch()) {
        $rowResult = $this->api->queryProduct(array('serverParams' => array('sku' => $sku, 'usertoken' => USER_TOKEN)));
        foreach($rowResult->result->imagesUrls->Image as $image) {
            $api_product_id = $product_id;
            $thumb_image = $image->smallurl;
            $medium_image = $image->middleurl;
            $original_image = $image->bigurl;
            $product_image->execute();
        }
    }
}

Can anyone help me why this is happening.

This is the message Fatal error: Call to a member function bind_param() on a non-object in $product_image->bind_param

kndwsu
  • 371
  • 1
  • 8
  • 21

1 Answers1

0

C.5.2.14. Commands out of sync

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

$product_info->execute() opens a mysql resource which prevents a new query. Unbuffered Queries might be a cause for that error. mysqli_stmt::prepare() returns unbuffered results by default:

Prepared statements return unbuffered result sets by default. [..] It is also possible to buffer the results of a prepared statement using mysqli_stmt_store_result().

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67