1
我想調用MS SQL Server上的存儲過程,但我得到一個空的結果。存儲過程將用戶標識作爲輸入參數,並輸出用戶的名稱,地址,城市,州和郵政編碼。PHP獲取空輸出與SQL Server存儲過程調用
這裏是存儲過程對SQL Server Management Studio中執行查詢:
USE [MYDB]
GO
DECLARE @return_value int,
@Name char(60),
@Address char(60),
@City char(30),
@State char(3),
@Zip char(10)
EXEC @return_value = [dbo].[spAddressByUserID]
@UserID = N'AB1234',
@Name = @Name OUTPUT,
@Address = @Addr1 OUTPUT,
@City = @City OUTPUT,
@State = @State OUTPUT,
@Zip = @Zip OUTPUT
SELECT @Name as N'@Name',
@Address as N'@Address',
@City as N'@City',
@State as N'@State',
@Zip as N'@Zip'
SELECT 'Return Value' = @return_value
GO
到目前爲止,我已經嘗試下面的代碼:
<?php
$conn = sqlsrv_connect($host, $connectionInfo);
$user_id = 'AB1234';
$myparams['UserID'] = $user_id;
$procedure_params = array(
array(&$myparams['UserID'], SQLSRV_PARAM_IN),
array(&$myparams['Name'], SQLSRV_PARAM_IN),
array(&$myparams['Address'], SQLSRV_PARAM_IN),
array(&$myparams['City'], SQLSRV_PARAM_IN),
array(&$myparams['State'], SQLSRV_PARAM_IN),
array(&$myparams['Zip'], SQLSRV_PARAM_IN)
);
$sql = "EXEC spAddressByUserID @UserID = ?, @Name = '', @Address = '', @City = '', @State = '', @Zip = ''";
$stmt = sqlsrv_prepare($conn, $sql, $procedure_params);
if(!$stmt) {
die(print_r(sqlsrv_errors(), true));
}
if(sqlsrv_execute($stmt)){
while($res = sqlsrv_next_result($stmt)){
print_r($res); // Gives Empty Output
}
}
else{
die(print_r(sqlsrv_errors(), true));
}
我覺得我所有的語法是正確的。但我得到空輸出。不知道我做錯了什麼。
編輯:
我已經修改了這樣的查詢參數數組,沒有成功:
$procedure_params = array(
array(&$myparams['UserID'], SQLSRV_PARAM_IN, 'SQLSRV_PHPTYPE_STRING'),
array(&$myparams['Name'], SQLSRV_PARAM_OUT, 'SQLSRV_PHPTYPE_STRING'),
array(&$myparams['Address'], SQLSRV_PARAM_OUT, 'SQLSRV_PHPTYPE_STRING'),
array(&$myparams['City'], SQLSRV_PARAM_OUT, 'SQLSRV_PHPTYPE_STRING'),
array(&$myparams['State'], SQLSRV_PARAM_OUT, 'SQLSRV_PHPTYPE_STRING'),
array(&$myparams['Zip'], SQLSRV_PARAM_OUT, 'SQLSRV_PHPTYPE_STRING')
);
我能夠成功地在SQL Management Studio中運行Proc。我也提到所有佔位符('?')在我的查詢中,但仍然得到空輸出。 –
@shasikanth我只注意到params被標記爲SQLSRV_PARAM_IN,而不是SQLSRV_PARAM_OUT。 – Canis
我將params指定爲SQLSRV_PARAM_OUT。但後來我得到了這個錯誤信息:一個無效的PHP類型被指定爲輸出參數。 DateTime對象,NULL值和流不能被指定爲輸出參數。 –