Bug #68359 Packets Out Of Order
Submitted: 13 Feb 2013 10:32 Modified: 15 Feb 2013 9:28
Reporter: James Hill Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.6.10 OS:Linux
Assigned to: CPU Architecture:Any

[13 Feb 2013 10:32] James Hill
Description:
When calling a stored procedure through mysqlnd in php, I get the following error when I fetch()

PHP Warning:  Packets out of order. Expected 1 received 40. Packet size=155

The exact same code and procedure on MySQL Server 5.1.52 works perfectly.

When I examine the packets in Wireshark, decoding as MySQL, Packet number 1 is marked as Malformed:
MySQL Protocol
Packet Length: 1
Packet Number: 1
[Malformed Packet: MySQL]
Expert Info (Error/Malformed): Malformed Packet (Exception occurred)
Message: Malformed Packet (Exception occurred)
Severity level: Error
Group: Malformed

How to repeat:
Create a stored procedure in Mysql 5.6.10 that returns a recordset

Call it in PHP 5.4, using mysqlnd with the following sequence of statements;
$stmt = $link->stmt_init();
$result = $stmt->prepare("call $query($argstr)");
$bindParameters($args);
$stmt->execute();
$stmt->store_result();
$data = $stmt->result_metadata();
$fields = array();
$out = array();
$count = 0;
while ($field = $data->fetch_field()) {
  $fields[$count] = &$out[$count];
  $count++;
}
call_user_func_array(array($stmt, 'bind_result'), $fields);
$data->close();
$stmt->fetch(); <------Error occurs here
$stmt->free_result();
[13 Feb 2013 21:18] Shane Bester
Why do you assume this is a bug?
[13 Feb 2013 23:09] James Hill
Well, I assumed that we would be able to call stored procedures returning recordsets without getting packets out of order or malformed in 5.6?

If I'm doing something wrong then it would be great if you could suggest something I can change....I would much rather it was my mistake and not a bug, simpler to fix! Something somewhere has changed between 5.1 and 5.6 as the same code calling the same stored procedure accessing the same data is now throwing an error.

Thanks in advance
[15 Feb 2013 8:45] Shane Bester
stored procedures return multiple resultsets, so you have to fetch them all. Nice example in the manual here, check if it works better:

http://php.net/manual/en/mysqli.quickstart.stored-procedures.php
[15 Feb 2013 9:28] James Hill
Thanks so much Shane, I hate to think how many hours I wasted on this!

Sorry for raising an issue that wasn't a bug, but hopefully this will help someone else as well.

Here is the working code:
$stmt = $link->stmt_init();
$result = $stmt->prepare("call $query($argstr)");
$bindParameters($args);
$stmt->execute();
do {
  $result = $stmt->get_result();
  var_dump($result->fetch_all());
  $result->free();
} while ($this->stmt->more_results() && $this->stmt->next_result());
[15 Feb 2013 9:41] Shane Bester
No problem, glad to help :)