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

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?


如果你想确保你总是得到字符串,你可以使用bindColumn()并为每一列指定数据类型

$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);
}

In so far as I remember, this depends on the DB engine.

Some time ago, PDO would return a t or f strings for boolean fields in Postgres, and I vaguely recall that it was returning a true or false boolean the last time I used it.

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

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

Doing so comes with a few strings attached, though. The warnings in the php manual are one. The inconsistent values returned from an engine to the next are another:

List of PHP native_type's for PDO getColumnMeta()

链接地址: http://www.djcxy.com/p/71964.html

上一篇: 如何存储PDO结果集

下一篇: PDO + MySQL总是返回字符串,但MsSQL呢?