存储过程返回多个结果集php,使用php / mysqli中的存储过程检索多个结果集

本文探讨了在PHP中使用mysqli和PDO处理MySQL存储过程返回的多个结果集的问题。虽然PDO的nextRowset()在某些情况下可能不适用于MySQL,但通过示例代码展示了如何使用mysqli库有效地读取所有结果。文章建议根据PHP版本选择合适的方法,并提及了面向对象风格的mysqli代码可能更具吸引力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我想你在这里错过了一些东西(下面还没有经过testing):

$stmt = mysqli_prepare($db, 'CALL multiples(?, ?)'); mysqli_stmt_bind_param($stmt, 'ii', $param1, $param2); mysqli_stmt_execute($stmt); // fetch the first result set $result1 = mysqli_use_result($db); // you have to read the result set here while ($row = $result1->fetch_assoc()) { printf("%d\n", $row['id']); } // now we're at the end of our first result set. mysqli_free_result($result1); //move to next result set mysqli_next_result($db); $result2 = mysqli_use_result($db); // you have to read the result set here while ($row = $result2->fetch_assoc()) { printf("%d\n", $row['id']); } // now we're at the end of our second result set. mysqli_free_result($result2); // close statement mysqli_stmt_close($stmt);

使用PDO你的代码如下所示:

$stmt = $db->prepare('CALL multiples(:param1, :param2)'); $stmt->execute(array(':param1' => $param1, ':param2' => $param2)); // read first result set while ($row = $stmt->fetch()) { printf("%d\n", $row['id']); } $stmt->nextRowset(); // read second result set while ($row = $stmt->fetch()) { printf("%d\n", $row['id']); }

但是我听说PDOStatement::nextRowset()没有使用MySQL PDO驱动程序实现,因此无法检索多个结果集:

PDO的nextRowset不能在MySQL上工作

pdo_mysql:存储过程调用返回单个行集阻止未来的查询

无法在Windows上使用PDO中的存储过程

所以,根据你的PHP版本,你必须坚持你的mysqli解决scheme。 顺便说一下:你故意使用程序风格吗? 使用面向对象的风格与mysqli会使你的代码看起来更吸引人(我个人的意见)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值