PDO + MySQL always returns rows, but what about MsSQL?

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

If not, is there a flag that forces PDO to always return rows (for consistency purposes)? or is it even better to return the native types for all values?

From what I can tell, Drupal allows you to use different databases using PDO. It performs the necessary transformations to make SQL statements compatible with various syntaxes. But how does this relate to data types in query results?

+6
source share
2 answers

If you want to make sure you always get rows, 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); } 
+2
source

As far as I remember, it depends on the DB mechanism.

Some time ago, PDO returned t or f strings for Boolean fields in Postgres, and I vaguely remember that it returned true or false boolean the last time I used it.

You can normalize the results to native types after checking getColumnMeta ():

http://us3.php.net/manual/en/pdostatement.getcolumnmeta.php

This happens with multiple lines. Warnings in the php manual is one thing. Inconsistent values ​​returned from the engine to the next are as follows:

PHP list native_type for PDO getColumnMeta ()

+1
source

Source: https://habr.com/ru/post/888216/


All Articles