PHP PDO中的DB2时间戳格式

时间:2015-07-09 16:43:38

标签: php pdo db2 odbc

我通过ODBC和EasySoft驱动程序让PHP与DB2(Ubuntu 14.04上的v10.5)进行了交谈。使用select id from new table(insert ....)格式进行查询时,我遇到时间戳格式问题。

这是我的表:

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

这与原始PDO插入一样正常:

$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"
]);

但是如果我尝试进行select id样式查询(使用相同的绑定):

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

我收到以下错误:

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

非常感谢任何帮助!

非常感谢

编辑:

将变量移动到查询本身而不是使用绑定可以正常工作:

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

2 个答案:

答案 0 :(得分:0)

这真的很奇怪。如果我尝试从CLP静态执行您的命令,一切正常:

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.

可能是ODBC驱动程序(或区域设置)以某种方式弄乱时间戳格式?为了确认这一点,我能想到的唯一方法是使用DB2 CLI和服务器跟踪,它们应该显示从客户端收到的时间戳数据。

在旁注中,我注意到created_atupdated_at的顺序在失败的情况下被颠倒了:

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

一个愚蠢的问题,但如果您修理订单会怎样?

答案 1 :(得分:0)

驱动程序供应商发布了修复程序。他们无法追查问题:

  

这需要一些跟踪,因为问题只发生在具有完全PDO-ODBC版本的PHP版本上。如果你的版本甚至超过.0.1,它也不会给出相同的结果。