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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用php / mysqli中的存储过程检索多个结果集脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个存储过程有多个结果集.如何提前到 mysqli的第二个结果集获得这些结果?

让我们说这是一个存储过程:

create PRocedure multiples( param1 INT,param2 INT )
BEgin

SELECT * From table1 WHERE id = param1;

SELECT * From table2 WHERE id = param2;

END $$

PHP是这样的:

$stmt = MysqLi_prepare($db,'CALL multiples(?,?)');

MysqLi_stmt_bind_param( $stmt,'ii',$param1,$param2 );

MysqLi_stmt_execute( $stmt );

MysqLi_stmt_bind_result( $stmt,$id );

那么这是我无法上班的部分.我已经尝试使用MysqLi_next_result移动到下一个结果集,但不能让它工作.我们确实可以使用MysqLi_Store_result和MysqLi_fetch_assoc / array / row,但是由于某些原因,所有的int都被返回为空字符串.

任何其他人都会遇到这个问题,并有解决办法吗?

我想你在这里遗漏了一些东西(以下未经测试):
$stmt = MysqLi_prepare($db,?)');
MysqLi_stmt_bind_param($stmt,$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()没有实现PDOStatement::nextRowset(),使得不可能检索多个结果集:

> PDO nextRowset not working on MySQL
> pdo_mysql: stored procedure call returning single rowset blocks future queries
> Can’t use stored procedures from PDO on Windows

所以,根据您的PHP版本,您必须坚持使用您的mysqli解决方案.顺便说一句:你是否有意地使用程序风格?使用面向对象的风格与MysqLi将使您的代码看起来更有吸引力(我的个人意见).

脚本宝典总结

以上是脚本宝典为你收集整理的使用php / mysqli中的存储过程检索多个结果集全部内容,希望文章能够帮你解决使用php / mysqli中的存储过程检索多个结果集所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。