我有一个存储过程有多个结果集.如何提前到
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都被返回为空字符串.
任何其他人都会遇到这个问题,并有解决办法吗?
我想你在这里遗漏了一些东西(以下未经测试):
原文链接:https://www.f2er.com/php/139459.html$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);
$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将使您的代码看起来更有吸引力(我的个人意见).