borrel
borrel

Reputation: 932

simple select returns unexpected result

running this code returns unexpected result

<?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;
}

returns

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        ',
    ),
)

as you can see the second statement fails

(note i'm running gentoo linux 64, and i'm connecting to SQL server 2012 on a win7 64 virtual machine)

Upvotes: 0

Views: 121

Answers (1)

david strachan
david strachan

Reputation: 7228

I get the second statement using MySQL. You may need to free up the connection to the server between statements using closecursor using 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());

PS fetch() only fetches 1 row. You should only have 1 id in var_dump()

Upvotes: 1

Related Questions