2017-02-16 161 views
0

我有一個問題,在成功運行該過程後顯示存儲過程的輸出...我希望每個人都可以幫助我....在這裏我的存儲過程..在Php MS中調用存儲過程和顯示輸出Sql

在我的SP

EXEC sp_mRetailerRegInfo_Add 'asdf','asdf','[email protected]','123', @RetailerId output, @ErrorCode output 

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

這是我的樣本數據,這是我的示例代碼..我可以運行的方法,但它沒有顯示輸出

<?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

回答

0

嗯,這是可能的。有一個叫做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); 
1

與存儲過程,你可以使用一個OUTPUT參數,你會在程序內的值賦給。過程完成後,您可以訪問該參數並根據需要使用該參數的內容。

您可以通過MSDN站點上的Return Data from a Stored Procedure閱讀以瞭解如何編寫存儲過程。

你會想看看this already answered question,它提供了一些關於如何從PHP訪問OUTPUT參數的細節。你會希望第二個答案在那裏:

第二個參數的執行需要是真實的,而不是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

+0

您好@gmiley:我只是想顯示成功後我的SP(像例如RetailerId)的輸出基於在我的question..thanks我的示例代碼插入數據 – zack