Home > Backend Development > PHP Tutorial > PHP PDO 调用存储过程 返回多个集合 和 输出参数 如何获取?

PHP PDO 调用存储过程 返回多个集合 和 输出参数 如何获取?

WBOY
Release: 2016-06-23 14:39:25
Original
1184 people have browsed it

select * from table;
select * from table1;
out $id;

如上:存储过程返回两个结果集和一个输出参数,如何在PHP中获得这两个结果集和输出参数?


回复讨论(解决方案)

一个个读取
见 PDOStatement::nextRowset 方法说明

一个个读取
见 PDOStatement::nextRowset 方法说明
如果只有多个集合我知道,但是有输出参数,如下

$stmt = $db->prepare("CALL pro_test(:name,@sp_result);");				$stmt->execute (array(':name'=>$name));		$outputArray = $db->query("select @sp_result")->fetch(PDO::FETCH_ASSOC);				print_r($outputArray["@sp_result"]);
Copy after login


存储过程返回一个集合,输出参数需要查询一次,但是到了这一步就出错了
$outputArray = $db->query("select @sp_result")->fetch(PDO::FETCH_ASSOC);
Error: SOAP-ENV:Server, string: Call to a member function fetch() on a non-object

我猜应该是之前存储过程返回了个结果,再查一个就出错了,如何解决?

这是手册上的示例

$sql = 'CALL multiple_rowsets()';$stmt = $conn->query($sql);$i = 1;do {    $rowset = $stmt->fetchAll(PDO_FETCH_NUM);    if ($rowset) { //想想这里为什么要先判断一下        printResultSet($rowset, $i);    }    $i++;} while ($stmt->nextRowset());
Copy after login
Copy after login

这是手册上的示例

$sql = 'CALL multiple_rowsets()';$stmt = $conn->query($sql);$i = 1;do {    $rowset = $stmt->fetchAll(PDO_FETCH_NUM);    if ($rowset) { //想想这里为什么要先判断一下        printResultSet($rowset, $i);    }    $i++;} while ($stmt->nextRowset());
Copy after login
Copy after login

你可能理解错我的意思了,我需要存储过程的结果集和存储过程返回的参数,不过已经解决了,还是去官网看的,谢谢关注,·· 后面还有很多问题
Note for MySQL: to use input/output parameters for stored procedures with PDO use PDO.Query() statement. For example: <?php $dbh->query("CAST SomeStoredProcedure($someInParameter1, $someInParameter2, @someOutParameter)"); $dbh->query("SELECT @someOutParameter"); ?> Or, if you want very much to use PDO.Prepare(), insert "SELECT @someOutParameter" in your stored procedure and then use: <?php $stmt = $dbh->prepare("CAST SomeStoredProcedure(?, ?)"); $stmt ->execute(array($someInParameter1, $someInParameter2)); ?>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template