|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-25 10:52 UTC] joshwaihi at gmail dot com
Description:
------------
When FALSE or NULL is passed to int column, PostgreSQL 8.3 fails with the following error: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""
It would seem the PDO postgreSQL driver is passing NULL and FALSE as a string. This is not however, the case for the MySQL Driver.
Reproduce code:
---------------
try {
$dbh = new PDO('pgsql:dbname=test;', 'myUser');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->query('CREATE TABLE test (id int not null, name text not null)');
$SQL = 'INSERT INTO test (id,name) VALUES (:id, :name)';
$query = $dbh->prepare($SQL);
$query->execute(array(':id' => 0, ':name' => 'This will work'));
$query->execute(array(':id' => FALSE, ':name' => 'This will not work'));
} catch (Exception $e) {
print $e->getMessage();
}
$dbh->query('DROP TABLE test');
Expected result:
----------------
no output
Actual result:
--------------
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 19:00:01 2025 UTC |
Based on what I've seen in gdb under Debian with php 5.2.9, this looks to be an issue where PHP believes it's an empty string, not a false boolean: (gdb) p *param $37 = {paramno = 0, name = 0x10dec80 ":id", namelen = 3, max_value_len = 0, parameter = 0x10deb10, param_type = PDO_PARAM_STR, driver_params = 0x0, driver_data = 0x0, stmt = 0x10dd398, is_param = 1} (gdb) p *param->parameter $38 = {value = {lval = 17684240, dval = 8.7371754568116058e-317, str = {val = 0x10dd710 "", len = 0}, ht = 0x10dd710, obj = {handle = 17684240, handlers = 0x0}}, refcount = 1, type = 6 '\006', is_ref = 0 '\0'} (gdb) If param->parameter was actually coming through as a IS_BOOL, the pdo_pgsql driver would act differently (it would translate it to a 't' or an 'f' respectively, which still isn't ideal since that would still break going into an integer column, but that's easily enough fixed by switching to '1' and '0', which PG will accept as booleans directly too).