【问题标题】:Calling Stored Procedure and Display Output in Php MS Sql在 PHP MS Sql 中调用存储过程并显示输出
【发布时间】:2017-07-05 00:00:33
【问题描述】:

我在成功运行程序后显示存储过程的输出有问题...希望大家可以帮助我....这里是我的存储过程..

$sql = "EXEC [dbo].[sp_mRetailerRegInfo_Add] '$var1','$var2','$var3','$var4','',''";

这是我的 SP 中的示例数据

 EXEC sp_mRetailerRegInfo_Add 'asdf','asdf','bacc@abc.com','123', @RetailerId output, @ErrorCode output

这是我的示例代码..我可以运行该过程但它不显示输出

<?php

$serverName = "localhost";

$connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pass");
$conn = sqlsrv_connect( $serverName, $connectionInfo);


if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}



//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------
$sql = "EXEC [dbo].[sp_mRetailerRegInfo_Add] '$var1','$var2','$var3','$var4','',''";


$result = sqlsrv_query($conn, $sql);
if (!$result) {
    echo 'Your code is fail.';
}
else {
    echo 'Success!';
    echo $result;
}

?>

输出将是这样的(ID0001),但我得到这样的输出

已建立连接。

成功!资源 ID #3

【问题讨论】:

    标签: php sql-server php-7


    【解决方案1】:

    嗯,这是可能的。有一个名为 mssql_bind 的函数用于将值绑定到 PHP 变量

    调用 SP 并存储输出。试试这样的

    DECLARE @appout int; -- variable to hold the data returned by SP.
    
    EXEC checkFollowing 10,20, @output = @appout OUTPUT; -- This is how i will call a SP.
    
    @appout - will hold the data returned by the procedure.
    

    所以你可以执行你的输出参数如下:

    $outVar1 = '';
    
    mssql_bind($procedure, "@appout", $outVar1, SQLVARCHAR, true);
    
    mssql_execute($procedure,true);
    
    print($outVar1);
    

    【讨论】:

      【解决方案2】:

      对于存储过程,您将使用一个OUTPUT 参数,您将在该过程中为其分配一个值。过程完成后,您可以访问该参数并根据需要使用它的内容。

      您可以通读 MSDN 网站上的Return Data from a Stored Procedure,了解如何编写存储过程。

      您需要查看this already answered question,其中提供了有关如何从PHP 访问OUTPUT 参数的一些详细信息。你会想要那里的第二个答案:

      execute 的第二个参数必须是 true,而不是 conn。这 应该工作:

      $conn = mssql_connect('server', 'user', 'pass');
      mssql_select_db('db', $conn);
      
      $procedure = mssql_init('usp_StoredProc', $conn);
      
      $tmpVar1 = 'value';
      $tmpVar2 = 'value2';
      
      $outVar1 = '';
      $outVar2 = '';
      
      mssql_bind($procedure, "@var1", $tmpVar1, SQLVARCHAR, false, false);
      mssql_bind($procedure, "@var2", $tmpVar2, SQLVARCHAR, false, false);
      
      mssql_bind($procedure, "@outVar1", $outVar1, SQLVARCHAR, true);
      mssql_bind($procedure, "@outVar2", $outVar2, SQLVARCHAR, true);
      
      mssql_execute($procedure,true);
      
      print($outVar1);
      print($outVar2);
      

      如果您可以为您的存储过程提供定义,我可以更具体地详细说明如何设置您的PHP 调用。

      【讨论】:

      • 嗨@gmiley:我只想在成功根据我的问题中的示例代码插入数据后显示我的SP输出(例如RetailerId)......谢谢
      猜你喜欢
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多