在PHP 5.4.4中使用PDO DBLIB调用3个存储过程失败

问题描述:

我尝试在PHP 5.4.4(Linux)中使用PDO DBLIB依次调用3个SQL Server 2000存储过程,并且在第二个查询时出现错误:致命错误:调用成员函数fetchAll ()在非对象上

I try to call 3 SQL Server 2000 Stored Procedures one after the other using PDO DBLIB in PHP 5.4.4 (Linux) and I get an error at the second query : Fatal error: Call to a member function fetchAll() on a non-object

第一个查询可以正常工作,并按预期返回结果.如果我移动查询顺序,则每次第一个查询成功而其他查询失败.

The first query works perfectly, returning results as expected. If I move query order, every time the first query succeeds and the others fail.

此外,当完全相同的代码在PHP 5.3.14服务器上运行时,一切正常.

Also, when the exact same code is run on a PHP 5.3.14 server, everything works great.

示例代码:

$dbh = new PDO ("dblib:host=myhost;dbname=mydb","user","pass");

$query = $dbh->query("EXEC dbo.storedProc1 'param1'");
$result = $query->fetchAll();
var_dump($result);

$query = $dbh->query("EXEC dbo.storedProc2 'param1'");
$result = $query->fetchAll(); // <-- Fails here
var_dump($result);

$query = $dbh->query("EXEC dbo.storedProc3 'param1'");
$result = $query->fetchAll();
var_dump($result);

是否有任何使此代码在PHP 5.4上运行的线索?

Any clue to make this code run on PHP 5.4 ?

PDO :: errorInfo给我该错误:Attempt to initiate a new Adaptive Server operation with results pending [20019] (severity 7) [EXEC dbo.storedProc2 'param1']

EDIT : PDO::errorInfo gives me that error : Attempt to initiate a new Adaptive Server operation with results pending [20019] (severity 7) [EXEC dbo.storedProc2 'param1']

此外,用SELECT(例如SELECT 1,SELECT 3和SELECT 3)调用query会得到相同的结果(给出第一个结果,其后为空)

Also, calling query with a SELECT (SELECT 1, SELECT 3 and SELECT 3 for example) gives the same result (first result is given, following are empty)

似乎与一个PHP错误有关,正如Capilé在评论中所注意到的

EDIT 2 : Looks like it's related to a PHP bug, as noticed by Capilé in the comments

我要费力地说,您的存储过程返回了多个结果集.要么是SQL Server 2000血腥地坚持要在游标为空时在下一个查询之前关闭游标.无论哪种方式,这都可以解决问题:

I'm going to do out on a limb and say that your stored procedure returns more than one result set. Either that, or SQL Server 2000 is bloody-mindedly insisting that you close the cursor before next query when it's empty. Either way, this should fix the problem:

$dbh = new PDO ("dblib:host=myhost;dbname=mydb","user","pass");

$results = array();
$query = $dbh->query("EXEC dbo.storedProc1 'param1'");
do {
  $results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($results);

$results = array();
$query = $dbh->query("EXEC dbo.storedProc2 'param1'");
do {
  $results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($results);

$results = array();
$query = $dbh->query("EXEC dbo.storedProc3 'param1'");
do {
  $results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($result);

请注意,当实际使用$results时,它将比您预期的要深一层,因为它可以存储多个结果集,并且这些结果集将存储在单独的键中.

Be aware that when actually using $results it will be one level deeper than you might expect, because it can store multiple result sets, and these would be stored in separate keys.