du67560 2011-01-21 10:48 采纳率: 0%
浏览 13
已采纳

PHP在同一个连接上调用第二个MySQL存储过程或查询?

I delved into MySQLi last night (only used normal mysql_ before that) to call MySQL stored procedures as part of a calendar script on my site. The SP that loads calendar events and hyperlinks the days works perfectly each time by using $(mysqli)->query("call SP"). This SP takes no parameters and returns a set of rows.
I want to add, in the same script, the option to click on one of the days and show details of the event that day. As this is the same script, I want to use the same MySQLi connection, but each time I try, the return value of the query is a Boolean(false). The second SP takes the date as a parameter, and I've constructed it before the query() call. Printing it to the web page, and running it from the MySQL CLI yields the expected data, so the SP works, and the user is authorised to execute SPs.
Out of interest, I tried replacing the SP call with the SP contents in query(), and again received a 'false' return, so it seems to me that MySQLi is not allowing me to perform two queries on the same connection. I've done some tinkering and tried free() on the first result set, to no avail. As I expected, if I closed and reopened the MySQLi connection, I could call the SP, but this seems to be very inefficient. Is there a specific sequence to perform multiple MySQL calls (not simultaneously) on the same connection? I've searched but only vaguely, as I don't really know what to search for. Thanks.

  • 写回答

1条回答 默认 最新

  • drfu29983 2011-01-21 12:21
    关注

    You need to call next_result after each sproc call as stored procedures return 2 resultsets: the data and a counter resultset.

    $db = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);
    
    $result = $db->query("call foo()");
    
    $db->next_result(); // using sprocs have to do this before next call !
    
    $result = $db->query("call bar()");
    
    $db->next_result(); 
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序