dongshi6969 2011-12-02 11:21
浏览 110
已采纳

在php调用中获取MS存储过程的输出(sqlsrv)

I am using the sqlsrv ms drivers for php, which work fine (tested with normal queries), I have also tested it with running a stored procedure to update table data which also works, know i want to use it to run a stored procedure and i want the response, how can this be done?

$server = "...the server address...";
$options = array("UID"=>"...the username...","PWD"=>"...the password...",
  "Database" => "...the database..."
  );

$conn = sqlsrv_connect($server, $options);

if ($conn === false) {die("<pre>".print_r(sqlsrv_errors(), true));}

$tsql_callSP = "{call ...the stored proc...( ?, ?)}";

$params = array( 
                 array("...first value in...", SQLSRV_PARAM_IN),
                 array("...second value in...", SQLSRV_PARAM_IN)
               );

$stmt3 = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt3 === false )
{
     echo "Error in executing statement 3.
";
     die( print_r( sqlsrv_errors(), true));
}

print_r( $stmt3); //attempting to print the return but all i get is Resource id #3
echo "test echo";

sqlsrv_free_stmt( $stmt3);
sqlsrv_close( $conn); 

I know i can use output parameter, but I will always receive multiple values from the stored proc.

  • 写回答

2条回答 默认 最新

  • doushan9415 2011-12-02 11:28
    关注

    Supposing that the stored procedure is returning the contents of one table with a single SELECT statement, using its output should be as simple as using the result of sqlsrv_query as you would any other selection query result (i.e. using sqlsrv_fetch_object/array on the result)! So the stored proc could look something like this:

    CREATE STORED PROCEDURE test
    AS
        -- do some other stuff here
        -- ...
        SELECT * FROM test
    GO
    

    And in your php:

    // establish db connection, initialize
    // ...
    
    $sql = "{call test}"
    $result = sqlsrv_query($conn, $sql);
    while (sqlsrv_fetch_object($result))
    {
         // do something with the data
         // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮