Call procedure works all right in MySQL terminal, but in PHP, caused Commands out of sync; you can\'t run this command nowCommands out of sync; you can\'t run this com
There seems to be a nasty bug (or feature) that is manifested when calling a stored procedure that returns a result set.. I.e. a stored procedure that ends with a select statement without an INTO clause (see example below).
The mysqli driver (propably) returns 2 result sets. The first being the one returned from the stored procedure and the second a dummy, empty result set. It is like a multiple query command was issued. One solution to this (that does not break on usual (e.g. SELECT) queries), is to consume this dummy result set after proccessing the legit one (the first).
Example php code
function do_query($con, $sql)
{
if ( !($result = mysqli_query($con, $sql)) )
throw new QueryException(mysqli_error($con));
if ($result === true)
return true;
while ($row = mysqli_fetch_assoc( $result )) {
// process rows
}
// Hack for procedures returning second dummy result set
while(mysqli_more_results($con)) {
mysqli_next_result($con);
// echo "* DUMMY RS \n";
}
}
Example stored procedure:
CREATE PROCEDURE selectStaleHeaders()
NOT DETERMINISTIC
SELECT TT.*
FROM one_pretty_table AS TT
LEFT JOIN another AS AN on TT.fk_id = AN.id
WHERE TT.id IS NULL;
i know it is late and there is already an answer, but i was getting the same message for a whole different reason, so i will leave my solution here:
it was actually a very silly error. It was just a typo in a parameter name.
My function had a parameter named preferable
:
create function call_client (pereferable int, client_id int) returns int
in the function body, i was using the parameter preferable
with the wrong name:
if prefered = 1 then
...
end if;
once i changed prefered
for preferable
it started working again.
C.5.2.14. Commands out of sync If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
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.
http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html
EDIT
I think you need to rewrite the getMostSimilar stored procedure, instead of using prepare and execute (which I thinks is fooling mysql) if you use the parameters in the procedure like in this example I think your error will be fixed.
HTH
This "bug" was happening to me with extremely simple procedure even inside phpmyadmin. The reason was that I was declaring general variables (without the @ prefix). I changed my variables to user-defined variables prefixed with @ and it was solved.