douyanzhou1450 2011-07-27 21:13
浏览 54
已采纳

检索存储过程返回的数据:Php&MS SQL SERVER

This question relates to:

PHP Version 5.3.6

Microsoft Drivers for PHP for SQL Server

I am trying to properly retrieve data from a stored procedure.

This question assumes the existence of the following stored procedure:

CREATE PROCEDURE test_procedure
AS
     BEGIN
           SET NOCOUNT ON
           --A bunch of SQL statements
           --More SQL statements
           SELECT 'Doctor Who Rules!'
     END

I've tried the following code which runs through all of my commands but does not return the data from the final SELECT.

$sql = "EXEC test_procedure;";
$result = sqlsrv_query($conn,$sql);
$next_result = sqlsrv_next_result($result); // returns a boolean
$row = sqlsrv_fetch_array($result);

Using sqlsrv_execute does not work with the above code either.

How can I return the data geneated by the stored procedure above via PHP?

Thank you.

Addendum #1 (Classic ASP Counterpart)

sqlCMD = "EXEC test_procedure;"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open _ConnectionString_Variable_
Set rs = conn.Execute(sqlCMD)

I would get back a recordset with one row that has one field with the data "Doctor Who Rules!"

  • 写回答

2条回答 默认 最新

  • dtu11716 2011-07-28 12:22
    关注

    I figured out the problem.

    The PHP code in my example will work properly if SET NOCOUNT ON is ommited from the Stored Procedure. With SET NOCOUNT ON in the procedure, there is no data stream back to PHP and thus, the results of the last SELECT never makes it back.

    Therfore, this Stored procedure...

    CREATE PROCEDURE test_procedure
         @foo varchar(25)
    AS
         BEGIN
              --A bunch of SQL statements
              --More SQL statements
              SELECT @foo
    END
    

    ...will work perfectly with this PHP...

    $sql = "EXEC test_procedure 'Dr. Who Rules!';";
    $result = sqlsrv_query($conn,$sql);
    $next_result = sqlsrv_next_result($result);
    $row = sqlsrv_fetch_array($result);
    

    You will end up with a recordset with the string "Doctor Who Rules!"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?