存入mysql的信息乱序,调用MySQL存储过程时数据包出现乱序错误

I'm trying to call a stored proc using PDO but am getting the following error when trying to do a fetch on the results.

Warning: Packets out of order. Expected 1 received 16. Packet size=163

My stored proc is using two cursors that I close before selecting from the temporary table. I'm suspecting this might be the problem because I can call my SP directly in MySQL and can see results. I also never had a problem with this SP when using the php_mysql extension before migrating to php_pdo_mysql.dll.

I'm also able to call my other simpler stored procs containing INPUT params in PHP using PDO and can fetch the results without any errors.

Here is the code that returns the error:

$db = new PDO('mysql:host='.__DB_HOST__.';dbname='.__DB_NAME__.';charset=utf8', __DB_USER__, __DB_PASS__);

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

/* DOES NOT WORK */

$queryResult = $db->prepare("CALL GetResults(:siteId,null)");

$siteId = 19;

$queryResult->bindValue(':siteId', $siteId, PDO::PARAM_INT);

$queryResult->execute();

$result = $queryResult->fetchAll(PDO::FETCH_ASSOC); // returns packets out of order warning

print_r($result);

I have this code in a Try/Catch block and no exception is being thrown. In fact, PHP is showing this as a Warning in the browser.

My Stored Procedure signature looks like this:

CREATE DEFINER=`root`@`localhost`

PROCEDURE `GetResults`(IN siteIdParam INT(11), IN siteSearchText VARCHAR(45))

I'm also not sure if the problem is with passing null as one of the params. Sometimes the first parameter passes null, sometimes it's the 2nd. But regardless it always works directly on the MySQL server.

I tried bindParam and bindValue, same results. I can also post my SP but it might be overkill.

Is there any way to turn on additional logging from the PDO extension?

Any ideas or suggestions? If you need more information, please let me know.

NOTE: I'm using PHP v5.5.4 and MySQL v5.6.14.

解决方案

After spending many hours trying to isolate parts of my code to solve this problem, I noticed that the error went away after setting the ATTR_EMULATE_PREPARES flag to true.

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

This tells PDO to emulate the prepared statements instead of natively by MySQL. From what I've been reading, it is generally recommended to turn this flag off (it's true by default) if you're using the most up to date version of MySQL and PHP. You can find more information on that in this SO article.

I do believe this to be a bug with MySQL (I had the problem up to version 5.6.17). There isn't much discussion on this particular problem so hopefully this saves someone else hours of troubleshooting. The problem is also discussed on this MySQL bug page, but the posted solution didn't help me in my situation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值