Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

I did so:

$serverName = "mySrv";
$db = new PDO("sqlsrv:Server=mySrv;Database=AdventureWorks", "myUser","mypass");

$db->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM);                         
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$tsql = "Select num from myTable";
$stmt = $db->prepare($tsql);

$stmt->execute();

$fun = $stmt->fetch();  

echo gettype($fun['num'])."<br>";
var_dump($fun['num']);

Result:

integer
int(496)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top