PDO存储过程的返回值

编程入门 行业动态 更新时间:2024-10-19 12:40:23
本文介绍了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);

推荐答案

查看MSDN,以获取有关如何正确绑定到此类呼叫的信息

您的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存储过程的返回值

本文发布于:2023-11-06 13:53:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   返回值   PDO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!