LinuxGnut LinuxGnut - 4 months ago 27
PHP Question

SQLSRV PDO Driver adding extra character to integers

I'm running IIS7 on a

Windows Server 2008 R2 server with PHP5.4 and SQLSRV
installed.

The basic problem is that when I try to retrieve certain values, extra/unknown characters are added to the returned value.

$options = array(PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_SYSTEM);
$this->db = new PDO("sqlsrv:server=$host;Database=$dbname",$user,$pass,$options);

$this->stmt =$this->connection->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));
$this->stmt->execute();

$this->obj = $this->stmt->fetch(PDO::FETCH_OBJ)
$this->obj->$column;


Where
$column
is something such as "
ID
".

When I run a var_dump on the returned value, it reads out
string(4) "455"
. If I have $strB set to
"455", $strA == $strB
evaluates to false...

I've tried
iconv and utf8_decode
on the returned value, as well as setting different
PDO
encodings, all with no success.

The database collation is set to
SQL_Latin1_General_CP1_CI_AS
. Any help on this would be much appreciated!

Answer

The issue apparently had to do with setting the cursor...

$this->stmt =$this->connection->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));

After removing the cursor settings from the options array, the unknown characters went away.