简体   繁体   中英

DB2 timestamp format in PHP PDO

I've got PHP talking to DB2 (v10.5 on Ubuntu 14.04) via ODBC and the EasySoft driver. I'm having trouble with the timestamp format when using select id from new table(insert ....) format for query.

Here is my table:

$create = "create table permissions (
        id int not null generated always as identity,
        name varchar(255) not null,
        display_name varchar(255),
        description varchar(255),
        created_at timestamp default current_timestamp not null,
        updated_at timestamp default current_timestamp not null
      )";

$pdo->prepare($create)->execute();

That works fine, as does the original PDO insert:

$sth = $pdo->prepare("insert into permissions (name, display_name, description, updated_at, created_at) values (?, ?, ?, ?, ?)");
$sth->execute([
"client_admin_dashboard",
"Client Admin Dashboard",
"Can view overview of team",
"2015-07-09 14:10:50.000",
"2015-07-09 14:10:50.000"
]);

But if I try to do the select id style query (with the same bindings):

$sth = $pdo->prepare("select id from new table(insert into permissions (name, display_name, description, updated_at, created_at) values (?, ?, ?, ?, ?))");

I get the following errors:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: -180 
[Easysoft][ODBC-DB2 Driver][DRDA] SQL0180N  The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007 (SQLExecute[4294967116] at 
/build/php5-RvVZKb/php5-5.6.10+dfsg/ext/pdo_odbc/odbc_stmt.c:254)' in 
/home/vagrant/repos/throwaway/test.php on line 65

PDOException: SQLSTATE[22007]: Invalid datetime format: -180 [Easysoft][ODBC-DB2 Driver][DRDA] SQL0180N  
The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007 
(SQLExecute[4294967116] at /build/php5-RvVZKb/php55.6.10+dfsg/ext/pdo_odbc/odbc_stmt.c:254) in /home/vagrant/repos/throwaway/test.php
on line 65

Call Stack:
    0.0007     226848   1. {main}() /home/vagrant/repos/throwaway/test.php:0
    0.1021     230648   2. PDOStatement->execute()     /home/vagrant/repos/throwaway/test.php:65

Any help would be much appreciated!

Many thanks

EDIT:

Moving the variables into the query itself instead of using bindings works fine:

$sth = $pdo->prepare("select id from new table(insert into permissions (name,
display_name, description, updated_at, created_at) values ( 
'client_admin_dashboard', 'Client Admin Dashboard', 'Can view overview of 
team', '2015-07-09 14:10:50.000', '2015-07-09 14:10:50.000'))")->execute();

It is truly weird. If I try to execute your command statically from CLP, everything works fine:

select id from new table(insert into permissions (name, display_name, description, updated_at, created_at) values ( 'client_admin_dashboard', 'Client Admin Dashboard', 'Can view overview of team', '2015-07-09 14:10:50.000', '2015-07-09 14:10:50.000'))

ID
-----------
          2

 1 record(s) selected.

Could it be that the ODBC driver (or regional settings) is somehow messing up the timestamp format? In order to confirm that, the only way I can think of is take DB2 CLI and server traces which should show the timestamp data received from the client.

On a side note, I have noticed that the order of created_at and updated_at is reversed in the failing case:

...(insert into permissions (..., updated_at, created_at)...

A stupid question, but what happens if you fix the order?

The driver vendor issued a fix. They had trouble tracking down the problem:

It took a bit of tracking down because the issue only occurs on your PHP version with that exact PDO-ODBC version. It doesn't give the same results if you are out by even .0.1 of a version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM