douxian8883 2018-07-24 19:23
浏览 61

MySQL程序没有返回第一行

Does the execute() statement perform an implicit fetch()? PHP does not show the first row of the returned result in the following simple code:

$query = 'CALL `MySP`()';

$stmt = $db->prepare($query);
$stmt->execute();        
$rows = $stmt->fetchAll();
return $rows;

$rows contains only 3 rows instead of actual 4; the first one is missing. The same SP returns 4 rows when run directly.

Edit

No luck yet after I tried everything suggested in the comments. Additionally I found that PDO can fail for very simple SPs too. Here is an example:

CREATE PROCEDURE `Test`()
BEGIN
  SELECT 123 as Col1
  UNION ALL
  SELECT 456 as Col1;
END

Try calling this proc from PHP. PDO throws a "no clue" exception when you try to fetch:

PDO error: “ SQLSTATE[HY000]: General error ” ...

  • 写回答

1条回答 默认 最新

  • dongma6326 2018-07-24 21:40
    关注

    Finally!

    This is so very stupid. Not sure who to blame. It ruined my entire night and turned out to be just the fact that I was using Count as alias for one of my columns. PDO somehow doesn't like this and as a result removes the first row of the result set. I'm pretty sure there would be a logical explanation of this unexpected behavior, since MySQL doesn't mind this alias.

    Anyway changing Count to RowCount or anything else fixed the behavior.

    Hope this helps someone down the road.

    Thank you everyone who worked with me on this one.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?