Using standard php5-pgsql driver it results to me that, at least with the default php settings on ubuntu 10.04, the result values are not mapped to the standard php types. That is
- postresql::integer => php::int
- postresql::real => php::float
- etc
And return the result values being cast to strings. It creates sometimes quite a lot of headache when the locale of system does not coincide with the locale of database etc.
Is it possible somehow to get the appropriate casting in php? If not, any insight on why would very welcome!
EDIT:
The PDO solved the “postresql::integer=>php::int”, but not for ex. “postresql::real=>php::float” it seems because of that PDO uses the native drivers that is pgsql as I understand. The issue with encoding is not a problem in my case, the only need is to map types exactly or at least as close as possible!
$sql = 'SELECT integer_field, numeric_field_10_2 FROM table';
foreach ($dbh->query($sql) as $row) {
var_dump($row);
}
----------------------------------------
array(4) {
["integer_field"]=>int(79)
["numeric_field_10_2"]=>string(6) "100.12"
}
Thanks, I.
This is an issue inherited from the C API, and that from the postgres client protocol. The results are provided as strings in the backend library. It shouldn’t be a problem with compatible encodings, but you’re right, it can be a problem.
You can use manual casting, or trust the automatics in php, but the real issue is just getting the encoding setup correctly. You can set postgres’ encoding on a per-database or per-cluster basis, I recommend using UTF-8, personally.
Converting an existing database is quite a hassle, it usually involves a full dump and full import into a new database.
pg_client_encoding will tell you the current postgres encoding, but won’t help with the php encoding. If you can’t use compatible encoding, maybe it would be worth creating a wrapper that uses iconv to convert the string data.
Finally, you could use an alternative client library. I believe the PDO postgres library for php handles this better.
http://us.php.net/manual/en/book.pdo.php