在查询中带有“declare”的PDO Dblib返回false

时间:2015-08-21 10:26:04

标签: php sql-server pdo

让我们检查一下这段代码:

$p = new PDO('dblib:host=HOST;dbname=DBNAME',USER,PASS);
$sql = "
declare @tutu int = 100;
select @tutu [tutu];
";
$stmt = $p->prepare($sql);
$stmt->execute();
var_dump($stmt->fetch(PDO::FETCH_ASSOC));

返回:

布尔(假)

1 个答案:

答案 0 :(得分:4)

使用nextRowset();

$p = new PDO('dblib:host=HOST;dbname=DBNAME',USER,PASS);
$sql = "
declare @tutu int = 100;
select @tutu [tutu];
";
$stmt = $p->prepare($sql);
$stmt->execute();
$stmt->nextRowset();
var_dump($stmt->fetch(PDO::FETCH_ASSOC));

返回:

array(1) {
  [0]=>
  array(1) {
    ["tutu"]=>
    string(3) "100"
  }
}