简体   繁体   English

PHP通过SQL Server存储过程调用获取空输出

[英]PHP getting empty output with SQL Server Stored Procedure call

I am trying to call a stored procedure on MS SQL Server, but i get an empty result. 我试图在MS SQL Server上调用存储过程,但结果为空。 The stored procedure takes the user id as input parameter, and outputs the name, address, city, state and zip code of the user. 该存储过程将用户ID作为输入参数,并输出用户的名称,地址,城市,州和邮政编码。

Here is the execution query of Stored Procedure on SQL Server Management Studio: 这是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

So far, i have tried the following code: 到目前为止,我已经尝试了以下代码:

<?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));
}

I think all my syntax is correct. 我认为我所有的语法都是正确的。 But i get empty output. 但是我得到空的输出。 Not sure what i am doing wrong. 不知道我在做什么错。

EDIT: 编辑:

I have modified the array parameters of query like this, with no success: 我已经修改了查询的数组参数,但没有成功:

$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')
);

Are you able to run the proc in SQL Management studio successfully? 您可以在SQL Management Studio中成功运行proc吗? It also looks like you are missing some placeholders in your SQL in PHP. 看起来您在PHP的SQL中似乎缺少一些占位符。 Only one ? 只有一个? in the query. 在查询中。 Perhaps this is better? 也许这样更好?

$sql = "EXEC spAddressByUserID @UserID = ?, @Name = '?', @Address = '?', @City = '?', @State = '?', @Zip = '?'";

I'm not very familiar with stored procs of this kind, but with parameterized placeholders in prepared statements, you need to specify all the placeholders, in the correct order. 我对这种存储的proc不太熟悉,但是在准备好的语句中使用参数化的占位符时,您需要以正确的顺序指定所有占位符。 From what you posted, it seems to me that you are not adding the placeholders in all the places it needs to go, and perhaps that is why you are not getting the output back? 从您发布的内容来看,在我看来,您并没有在需要使用的所有位置添加占位符,也许这就是为什么您没有收回输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM