与fetch_all一起使用时,存储过程导致mysqli问题 [英] Stored Procedure causing problems with mysqli when used with fetch_all

查看:55
本文介绍了与fetch_all一起使用时,存储过程导致mysqli问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把这个问题从本质上分解了,但是仍然有问题.

I have broken down this issue to it's essence but am still having problems.

当我尝试使用fetch_all获取存储过程的结果时,我将结果按预期方式返回到数组,但是随后的mysqli调用抛出命令不同步"错误.

When I try and use fetch_all to get the results of a stored procedure, I get results returned to array as expected, but subsequent mysqli calls throw "command out of sync" errors.

我什至尝试将存储过程简化为...

I even tried simplifying my stored procedure to...

CREATE PROCEDURE testp()
BEGIN
   SELECT * FROM tbl
END

我把桌子缩小了,有10行3列.

I made the table small, 10 rows 3 cols.

我使用以下PHP

$query = "CALL testp()";
$result = $db->query($query);
$rows = $result->fetch_all(MYSQL_ASSOC);
$result->free();
print_r($rows);
$query = "SELECT * FROM tbl WHERE id = ?";
$stmt = $this->db->prepare($query);
echo $this->db->error;
$stmt->close();

Echo的命令不同步;您现在不能运行此命令" 并抛出 致命错误:在非对象上调用成员函数close()

Echo's "Commands out of sync; you can't run this command now" and throws Fatal error: Call to a member function close() on a non-object

$ rows预期输出

$rows outputs as expected

有什么想法吗?

推荐答案

说实话,这实际上就是mysqli的工作方式! 事情以2为批次发生,这意味着您需要先清除最后的结果,然后再执行其他操作.如果没有->错误!

This is actually how mysqli works to be honest! Things happen in batches of 2, which means you need to clear the last result before doing anything else. If you don't -> ERROR!

您可以使用next_result()来实现,示例如下:

You can achieve that by using next_result(), example follows:

while($this->_mysqli->next_result());

..然后成功进行代码.上面的示例恰好在fetch_assoc_进程运行之后才返回数组.

..then carry on with your code successfully. The above example would fit just after the fetch_assoc_ process is run before returning the array.

(我知道这是3个月前问的,但迟到总比没有好;)

(I know this was asked 3 months ago, but rather late than never ;)

这篇关于与fetch_all一起使用时,存储过程导致mysqli问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆