PDO always returns field values as strings when using MySQL. Is PDO consistent when using another database like MSSQL?

If not, is there a flag which forces PDO to always return strings (for purpose of consistency)? or better still to return native types for all values?

From what I can tell, Drupal makes it possible to use different databases using PDO. It performs the necessary conversions to make SQL statements compatible with the varying syntaxes. But how does it deal with data types in query results?


This might be useful: blogs.msdn.com/b/sqlphp/archive/2011/01/25/…

Written by Spudley

Accepted Answer

If you want to make sure that you always get strings you can use bindColumn() and specify the data type for each column

$sql = 'SELECT id, name FROM test';
$stmt = $dbh->query($sql);
/* Bind by column number */
$stmt->bindColumn(1, $id, PDO::PARAM_STR); //or PDO::PARAM_INT
$stmt->bindColumn(2, $name, PDO::PARAM_STR);
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
  var_dump($id); var_dump($name);
Written by silverskater
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki