1

MySQL Error: Commands out of sync; you can't run this command now

I get the above error when I try to execute an SQL Procedure more than once each time.

I've found that it has something to with the "store result", so I've been trying to figure it out for a while, but I haven't succeeded yet.

Here is my PHP code:

$query = "CALL GetObjectsValue(".$FormID.")";

if( $res = $mysqli->query($query) )
{
    $mysqli->store_result();

    while ($row = $res->fetch_assoc()) 
    {

        ...

    }
    $res->free();
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Dan
  • 181
  • 5
  • 15

2 Answers2

0

For some reason, for recordset from stored procedures one has to loop through it using next_result() command. This is not the case if recordset comes directly by execution of SQL query (SELECT).

$query="CALL GetObjectsValue($FormID)";
if($res=$mysqli->query($query))
{
  do
  {
    $row = $res->fetch_assoc()
    ...
  }
  while($mysqli->next_result()); //loop through recordset

  $res->free();
}
sbrbot
  • 6,169
  • 6
  • 43
  • 74
-2

Change

$query = "CALL GetObjectsValue(".$FormID.")";     

to

$query = "CALL GetObjectsValue("$FormID")";  

Notice the $FormID without the dots.

Litmus
  • 10,558
  • 6
  • 29
  • 44
felipe
  • 1