Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SET时,SQL Server 2005 T-SQL和PDO没有输出_Php_Sql Server_Tsql_Sql Server 2005_Pdo - Fatal编程技术网

Php 使用SET时,SQL Server 2005 T-SQL和PDO没有输出

Php 使用SET时,SQL Server 2005 T-SQL和PDO没有输出,php,sql-server,tsql,sql-server-2005,pdo,Php,Sql Server,Tsql,Sql Server 2005,Pdo,我使用PDO连接到SQLServer2005数据库,在使用PHP运行某些类型的查询时不会得到输出,但在SQLServerManagementStudio中直接运行查询时会得到输出 当我运行此命令时: $conn = new PDO(...); $statement = $conn->prepare(" DECLARE @testvar VARCHAR(10) SELECT 'hello world' "); $result = $statement->execut

我使用PDO连接到SQLServer2005数据库,在使用PHP运行某些类型的查询时不会得到输出,但在SQLServerManagementStudio中直接运行查询时会得到输出

当我运行此命令时:

$conn = new PDO(...);

$statement = $conn->prepare("
    DECLARE @testvar VARCHAR(10)
    SELECT 'hello world'
");

$result = $statement->execute();

echo $result ? "Success" : "Failure";
echo "<br>";

print_r($conn->errorInfo());
echo "<br>";

die(print_r($statement->fetchAll()));
但是,如果我将查询设置为将
@testvar
设置为某个值,则可以内联:

DECLARE @testvar VARCHAR(10) = 'test'
SELECT 'hello world'
或使用SET:

DECLARE @testvar VARCHAR(10)
SET @testvar = 'test'
SELECT 'hello world'
当运行
$statement->fetchAll()
时,我不再获得
“hello world”
,并且似乎没有错误:

Success
Array ( [0] => 00000 [1] => 0 [2] => (null) [0] (severity 0) [] [3] => 0 [4] => 0 ) 
Array ( ) 1

当我直接在SQLServerManagementStudio中尝试上述三个查询时,这三个查询都可以正常工作并正确返回“HelloWorld”。

找到了答案。使用
SET
(与
DECLARE
内联或作为显式
SET
)会导致结果现在在PDO术语中有多个行集。我要查找的
“hello world”
字符串不在第一行集中

与此相反:

die(print_r($statement->fetchAll()));
遍历所有行集:

do {
    print_r($statement->fetchAll());
    echo "<br>";
}
while ($statement->nextRowset());
do {
    print_r($statement->fetchAll());
    echo "<br>";
}
while ($statement->nextRowset());
Array ( ) // rowset 1
Array ( [0] => Array ( [] => hello world ) )  // rowset 2