【问题标题】:Multiple Recordset/Columns echo to PHP from MSSQL stored procedure多个记录集/列从 MSSQL 存储过程回显到 PHP
【发布时间】:2017-02-07 15:29:47
【问题描述】:

我正在尝试使用 PHP 运行 MSSQL 存储过程,然后获取结果并将它们回显到页面。结果输出为带有一堆列的两个不同的记录集。见screenshot

我已经修改了一些变量名和输出来保护一些敏感信息,但是下面的 SQL 代码正是我在 MSSQL Server Management Studio 中运行的,它返回了上面链接的输出屏幕截图。

我还通过 PHP 尝试了以下操作,但没有返回任何结果:

if (!@mssql_connect($host,$username,$password)) {
    $error = $db_connect_error;
    echo $error;
}
@mssql_select_db($dbname);
$stmt=mssql_init("[dbo].[stored_procedure_name]");
mssql_bind($stmt, "@StartTime",    $start_last, SQLVARCHAR, FALSE);
mssql_bind($stmt, "@EndTime",  $end_last, SQLVARCHAR, FALSE);
mssql_bind...continued for all variables
$result = mssql_execute($stmt);
$arr = mssql_fetch_row($result);
print_r($arr);

我尝试了通过谷歌搜索找到的其他几种不同的方法来获取数据以在 PHP 中回显或保存到数组中,但我没有尝试过。

在 MSSQL Server Management Studio 中运行没有问题的 MSSQL 语句,这是我找到存储过程并从 MSSQL SMS 运行时的默认格式:

DECLARE @RC int
DECLARE @StartTime datetime
DECLARE @EndTime datetime
DECLARE @a varchar(4000)
DECLARE @b varchar(4000)
DECLARE @c varchar(50)
DECLARE @d varchar(50)
DECLARE @e varchar(50)
DECLARE @f varchar(50)
DECLARE @g int
DECLARE @h int
DECLARE @i int
DECLARE @j int
DECLARE @k int
DECLARE @l int
DECLARE @m int
DECLARE @n int
DECLARE @o int
DECLARE @p int
DECLARE @q int
DECLARE @r varchar(100)
DECLARE @s int
DECLARE @t float
DECLARE @u float
DECLARE @v float
DECLARE @w int

-- TODO: Set parameter values here.

SET @StartTime = '2017-02-01 07:30'
SET @EndTime = '2017-02-01 19:30'
SET @a = 'string'
SET @b = 'string'
SET @c = 'string'
SET @d = 'string'
SET @e = 'string'
SET @f = 'string'
SET @g = 105
SET @h = 108
SET @i = 101
SET @j = 147
SET @k = 124
SET @l = 141
SET @m = 103
SET @n = 186
SET @o = 185
SET @p = 113
SET @q = 182
SET @r = 'string'
SET @s = 77
SET @t = 101.5
SET @u = 1000
SET @v = 25.4
SET @w = 1

EXECUTE @RC = [dbo].[spLocal_RptCvtgLinestatus] 
   @StartTime
  ,@EndTime
  ,@a
  ,@b
  ,@c
  ,@d
  ,@e
  ,@f
  ,@g
  ,@h
  ,@i
  ,@j
  ,@k
  ,@l
  ,@m
  ,@n
  ,@o
  ,@p
  ,@q
  ,@r
  ,@s
  ,@t
  ,@u
  ,@v
  ,@w

【问题讨论】:

  • 你的连接在哪里?另外,你真的应该考虑更新到mysqli_*PDO
  • 糟糕,我没有添加连接信息,我已将其添加到其他 PHP 代码上方的原始帖子中。

标签: php sql-server stored-procedures


【解决方案1】:

首先,正如我的评论所提到的,您应该考虑更新到 mysqli_*PDO

对于您当前的设置,您缺少看起来像的连接

$connection = mssql_connect($servername, $username, $password);
//Select a db
mssql_select_db('your_db_name', $connection);

//Use the connection. I do not think you need to fully qualify the sproc name.
$stmt = mssql_init('stored_procedure_name', $connection);

另外,在执行 mssql_fetch_row 时,它只返回一行,因此您需要循环它:

$data = array();
while ($row = mssql_fetch_row($result)){
    $data[] = $row;
}

print_r($data);

【讨论】:

  • 啊,成功了!谢谢!是的,由于不同的原因,我认为此时更新不是一个可行的选择,但我想添加它以解决一些 NULL 错误,我必须添加: mssql_query('set ANSI_NULL_DFLT_ON',$connection) ;
【解决方案2】:

要遍历多个记录集,您可以试试这个..

$sql = "EXEC spr_MyStoredProcedure @param1 = 6, @param2 = 5;";
$conn = sqlsrv_connect( $hostname, array( "Database"=>$database, "UID"=>$username, "PWD"=>$password ));
if( !$conn) {
    die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_query($conn, $sql, null);
if($result ) {
    while( $obj = sqlsrv_fetch_object($result)){ // check the first recordset
        print_r($obj);
    }
    while( sqlsrv_next_result($result)){ // if there are any more recordsets
        while( $obj = sqlsrv_fetch_object($result)){ // loop them too
            print_r($obj);
        }   
    }
}
sqlsrv_free_stmt( $result ); 
sqlsrv_close( $conn );

这尚未在生产环境中进行测试,因此您的里程可能会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 2010-11-28
    • 2013-09-01
    • 2011-03-05
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多