当前位置:Gxlcms > PHP教程 > php-PHP执行存储过程,不能正确返回数据集问题

php-PHP执行存储过程,不能正确返回数据集问题

时间:2021-07-01 10:21:17 帮助过:7人阅读

php存储数据库

建有一存储过程:
Create procedure T_SP
@X int,
@Y int

AS

IF OBJECT_ID('TEMPEB.#DATA') IS NOT NULL    DROP TABLE #DATACREATE TABLE #DATA(X INT,Y INT)INSERT INTO #DATA(X,Y) VALUES(@X,@Y)SELECT * FROM #DATA

GO

用以下PHP代码希望返回存储过程执行的数据集:

$serverName = "127.0.0.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"BandaiQCE", "UID"=>"sa", "PWD"=>"justgo");
$lik = sqlsrv_connect( $serverName, $connectionInfo);
if( !$lik ) {
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}

echo "
"."$serverName";

//-----------------------------------------------------------------------
$sql = "{call T_SP (?,?)}";
$X = 100;
$Y = 80;
$params = array(
array(&$X, SQLSRV_PARAM_IN),
array(&$Y, SQLSRV_PARAM_IN)
);
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$stmt = sqlsrv_query( $lik, $sql, $params);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}

$row_count = sqlsrv_num_rows( $stmt );

if ($row_count === false)    echo "Error in retrieveing row count.";else    ECHO "
".$row_count."
";

$serverName = "127.0.0.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"BandaiQCE", "UID"=>"sa", "PWD"=>"justgo");
$lik = sqlsrv_connect( $serverName, $connectionInfo);
if( !$lik ) {
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}

echo "
"."$serverName";

//-----------------------------------------------------------------------
$sql = "{call T_SP (?,?)}";
$X = 100;
$Y = 80;
$params = array(
array(&$X, SQLSRV_PARAM_IN),
array(&$Y, SQLSRV_PARAM_IN)
);
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$stmt = sqlsrv_query( $lik, $sql, $params, $options);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}

$row_count = sqlsrv_num_rows( $stmt );

if ($row_count === false)    echo "Error in retrieveing row count.";else    ECHO "
".$row_count."
";

?>

执行结果报如下错误:
Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]正在直接执行 SQL;无游标。 [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]正在直接执行 SQL;无游标。 ) )

很明显:错误的原因在于存储过程执行的问题,为何未能返回数据集?也就是为何没有按预期返回如下结果?
X    Y
100   80

请高手帮忙解答!谢谢!

人气教程排行