PDO存储过程的返回值 [英] PDO Stored Procedure return value
问题描述
我正在使用一个返回错误代码的SQL Server存储过程;这是SP的一个非常简单的代码段.
I'm working with a SQL Server stored procedure that returns error codes; here is a very simple snippet of the SP.
DECLARE @ret int
BEGIN
SET @ret = 1
RETURN @ret
END
我可以使用以下方式获得带有mssql扩展名的返回值:
I can get the return value with the mssql extension using:
mssql_bind($proc, "RETVAL", &$return, SQLINT2);
但是,我无法弄清楚如何在PDO中访问返回值.我不希望使用OUT参数,因为已经编写了许多存储过程.这是我当前如何在PHP中调用该过程的示例.
However, I can't figure out how to access the return value in PDO; I'd prefer not to use an OUT parameter, as alot of these Stored Procedures have already been written. Here is an example of how I am currently calling the procedure in PHP.
$stmt = $this->db->prepare("EXECUTE usp_myproc ?, ?");
$stmt->bindParam(1, 'mystr', PDO::PARAM_STR);
$stmt->bindParam(2, 'mystr2', PDO::PARAM_STR);
$rs = $stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
推荐答案
Your PHP code should probably be tweaked to look more like this. This may only work if you're calling through ODBC, which is honestly the strongly preferred way to do anything with SQL Server; use the SQL Native Client on Windows systems, and use the FreeTDS ODBC driver on *nix systems:
<?php
$stmt = $this->db->prepare("{?= CALL usp_myproc}");
$stmt->bindParam(1, $retval, PDO::PARAM_STR, 32);
$rs = $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "The return value is $retval\n";
?>
关键是返回值可以绑定为OUT参数,而不必重组存储过程.
The key thing here is that the return value can be bound as an OUT parameter, without having to restructure the stored procedures.
这篇关于PDO存储过程的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!