Reputation: 2233
For SQL Server integer data types it seems PDO_SQLSRV returns strings whereas SQLSRV returns integers. My already written app does explicit checking (===) in several places so that means PDO_SQLSRV not a drop in replacement for SQLSRV. Normally I wouldn't be considering PDO - however Doctrine seems to use PDO and I couldn't find a Microsoft SQLSRV driver for it (enlighten me, please!)
Here is the sample code with the responses:
PDO
$sql = "SELECT Top 1 * FROM Users";
$stmt = $conn->query($sql);
$things = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($things);
Data:
'ID' => string '344' (length=3)
'UserName' => string 'admin ' (length=30)
SQLSRV
$sql = "SELECT Top 1 * FROM Users";
$things = sqlsrv_query($conn, $sql);
$thingsArray = [];
while( $row = sqlsrv_fetch_array($things, SQLSRV_FETCH_ASSOC))
{
$thingsArray[] = $row;
}
var_dump($thingsArray);
Data:
'ID' => int 344
'UserName' => string 'admin ' (length=30)
So - right now I think I have two choices: carefully release this and make sure that I'm typecasting integers or loosening my comparisons, or write/find a Doctrine SQLSRV library.
Does anyone know of a setting for PDO_SQLSRV that will return integers as integers? I've checked the docs, but can't seem to find anything.
Also SQLSRV returns dates as native PHP DateTime objects. Kinda cool, but not important.
Upvotes: 0
Views: 886
Reputation: 191
The PHP PDO spec states that all fields are returned as strings, so pdo_sqlsrv is just obeying the spec.
Upvotes: 1