PDO存储过程的返回值 [英] PDO Stored Procedure return value

查看:98
本文介绍了PDO存储过程的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个返回错误代码的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);

推荐答案

您的PHP代码可能应该进行调整,使其看起来更像这样.仅当您通过ODBC调用时,这种方法才可能起作用.老实说,这是使用SQL Server进行任何操作的首选方法.在Windows系统上使用SQL Native Client,在* nix系统上使用FreeTDS ODBC驱动程序:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆