0

运行此代码返回意外结果

<?php
error_reporting(-1);
try{
    $pdo = new PDO('odbc:Driver={SQL Server Native Client 11.0};Server=192.168.178.11;Database=test;','sa', 'secret');
    $s = $pdo->prepare('SELECT * FROM test');
    if($s->execute()){
        var_dump($s->fetchAll());
    }
    $s = $pdo->prepare('SELECT * FROM test WHERE id = ?');
    if($s->execute(array(1))){
        var_dump($s->fetchAll());
    }
}catch(Exception $e){
    echo $e->xdebug_message;
}

返回

array (
    0 => 
    array (
      'id' => '1',
      0 => '1',
      'test' => 'test      ',
      1 => 'test      ',
    ),
    1 => 
    array (
      'id' => '2',
      0 => '2',
      'test' => 'test2     ',
      1 => 'test2     ',
    ),
    2 => 
    array (
      'id' => '3',
      0 => '3',
      'test' => 't3        ',
      1 => 't3        ',
    ),
)

如您所见,第二个语句失败

(注意我正在运行 gentoo linux 64,并且我在 win7 64 虚拟机上连接到 SQL Server 2012)

4

1 回答 1

1

我使用 MySQL 得到第二个语句。您可能需要在使用 SQL Server的closecursor的语句之间释放与服务器的连接。

$pdo = new PDO('odbc:Driver={SQL Server Native Client 11.0};Server=192.168.178.11;Database=test;','sa', 'secret');
    $s = $pdo->prepare('SELECT * FROM test');
    if($s->execute()){
        var_dump($s->fetch());
    }
    $s->closeCursor();// ADD THIS for SQL Server
    $s = $pdo->prepare('SELECT * FROM test WHERE id = ?');
    if($s->execute(array(1))){
        var_dump($s->fetch());

PSfetch()仅获取 1 行。您应该只有 1 个 idvar_dump()

于 2013-06-29T18:52:45.530 回答