0

This seems to be a Windows specific issue a few people have had, without a satisfactory outcome. I haven't tried the code on my Linux server yet.

When using a prepared statement to call a stored procedure multiple times crashes Apache. The stored procedure appears to be returning two results, even for a simple SELECT * FROM users making the next execute not work.

The workaround I'm using now is $mysqli->next_result(); between each execute.

My question, has anyone experienced this? And is there a fix, is this a bug or am I just doing something wrong?

Windows 7 wamp server -Apache 2.4.2 -Mysql 5.5.24 -PHP 5.4.3

/* MySQL code */
DELIMITER $$
PROCEDURE `get_data`(IN var1 VARCHAR(45),IN var2 VARCHAR(45) )
BEGIN
    SELECT * 
    FROM data_table
    WHERE `data1` = var1 AND `data2` = var2;
END
/* End MySQL code */
<?php
if( !($stmt = $mysqli->prepare("CALL get_data(?,?)")) )
    die("Prepare failed: (" . $stmt->errno . ") " . $stmt->error);


$var1 = "some_data_1";
$var2 = "some_data_2";
if( $stmt->bind_param("ss", $var1, $var2) )
{
    if( $stmt->execute() )
    {
        echo "Execute one complete.";
    }
    else
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
    echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error;


/* Current fix to clear stray results sent from the stored procedure */
$stmt->next_result();


$var2 = "some_data_555";
if( $stmt->bind_param("ss", $var1, $var2) )
{
    if( $stmt->execute() )
    {
        echo "Execute two complete.";
    }
    else
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else
    echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error;
?>

The code is obviously modified for debugging and stackoverflow.

The exact code with a normal query, IE SELECT * FROM users works fine, until you call a stored procedure, IE CALL get_data(?,?)

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
Bradmage
  • 1,233
  • 1
  • 15
  • 41
  • Your procedure will most likely not return any data at all, because `data1` can not have two different values at the same time. And why are you using a procedure at all in this case? Post your real code, since it's also not clear what you mean with "The sproc appears to be returning two results". – fancyPants Apr 17 '13 at 12:40
  • I changed data1 to data2, that is from actual code, just with variable names changed. Prepared statements can only handle receiving one result set, and the prepared statement returns two. From other peoples experience it seems like it returns the data, as well as a "query ok" result. Breaking everything. – Bradmage Apr 17 '13 at 12:47
  • This [solved question](http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) might help. There are some more results, than expected. – BlitZ Apr 18 '13 at 03:02
  • 1
    Also, read [`handling result sets`](http://php.net/manual/en/mysqli.quickstart.stored-procedures.php) and [`use of prepared statements`](http://php.net/manual/en/mysqli.quickstart.stored-procedures.php) sections of [`php.net`](http://php.net), especially last part of example 3 & 4 (output). There a [`solution`](http://stackoverflow.com/questions/16029729/mysql-error-commands-out-of-sync-you-cant-run-this-command-now/16029814#16029814) I`ve posted before. – BlitZ Apr 18 '13 at 03:23
  • @CORRUPT Thanks, the PHP page was helpful, I overlooked '..there are additional stored procedure result sets hidden from the user..' So it is normal behavior, doesn't explain why it crashes Apache though and not returning an error. Your solution to the Out Of Sync error would work, it only addresses retrieving multiple result sets from a query. Which isn't what my question was, it was doing multiple executes on one prepared statement. So it looks like $stmt->next_result(); is required, but more appropriatly after reading example 3 $result->free(); – Bradmage Apr 19 '13 at 00:45
  • **@Bradley Forster**, yep, there is more to it. Is your Apache configured to run PHP in CGI-mode or as Apache module? Just curious. – BlitZ Apr 19 '13 at 02:46
  • @CORRUPT I'm pretty sure wampserver default configuration is CGI. At least now I know I do have to free the result. Was just frustrating figuring out what the problem was in the first place, not being able to trace the error. Always used prepared statements, but only just started using stored procedures. – Bradmage Apr 20 '13 at 02:00

0 Answers0