运行此代码将返回意外结果

<?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)

最佳答案

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

$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()仅获取一行。在var_dump()中只能有一个id

关于php - 简单选择返回意外结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17383103/

10-08 23:49