使用 SET 时 SQL Server 2005 T-SQL 和 PDO 没有输出
No output from SQL Server 2005 T-SQL and PDO when using SET
我正在使用 PDO 连接到 SQL Server 2005 数据库,并且当 运行 使用 PHP 进行某些类型的查询时没有得到输出,但是当 运行直接在 SQL Server Management Studio 中查询。
当我运行这个:
$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()));
我得到输出。
Success
Array ( [0] => 00000 [1] => 0 [2] => (null) [0] (severity 0) [] [3] => 0 [4] => 0 )
Array ( [0] => Array ( [] => hello world ) ) 1
但是,如果我更改查询以将 @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
当我直接在 SQL Server Management Studio 中尝试上面的所有三个查询时,所有三个查询都有效并且 return "hello world"
正确。
想通了。使用 SET
(与 DECLARE
内联或作为显式 SET
)导致结果现在在 PDO 术语中具有多个行集。我要查找的 "hello world"
字符串不在第一个行集中。
而不是这个:
die(print_r($statement->fetchAll()));
遍历所有行集:
do {
print_r($statement->fetchAll());
echo "<br>";
}
while ($statement->nextRowset());
现在结果是这样的:
Array ( ) // rowset 1
Array ( [0] => Array ( [] => hello world ) ) // rowset 2
我正在使用 PDO 连接到 SQL Server 2005 数据库,并且当 运行 使用 PHP 进行某些类型的查询时没有得到输出,但是当 运行直接在 SQL Server Management Studio 中查询。
当我运行这个:
$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()));
我得到输出。
Success
Array ( [0] => 00000 [1] => 0 [2] => (null) [0] (severity 0) [] [3] => 0 [4] => 0 )
Array ( [0] => Array ( [] => hello world ) ) 1
但是,如果我更改查询以将 @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
当我直接在 SQL Server Management Studio 中尝试上面的所有三个查询时,所有三个查询都有效并且 return "hello world"
正确。
想通了。使用 SET
(与 DECLARE
内联或作为显式 SET
)导致结果现在在 PDO 术语中具有多个行集。我要查找的 "hello world"
字符串不在第一个行集中。
而不是这个:
die(print_r($statement->fetchAll()));
遍历所有行集:
do {
print_r($statement->fetchAll());
echo "<br>";
}
while ($statement->nextRowset());
现在结果是这样的:
Array ( ) // rowset 1
Array ( [0] => Array ( [] => hello world ) ) // rowset 2