PHP PDO 中的 DB2 时间戳格式
DB2 timestamp format in PHP PDO
我已经 PHP 通过 ODBC 和 EasySoft 驱动程序与 DB2(Ubuntu 14.04 上的 v10.5)通信。我在使用 select id from new table(insert ....)
格式进行查询时遇到时间戳格式问题。
这是我的 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();
效果很好,原始 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();
真是奇怪。如果我尝试从 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_at
和 updated_at
的顺序在失败的情况下颠倒了:
...(insert into permissions (..., updated_at, created_at)...
一个愚蠢的问题,但是如果你修改顺序会怎样?
驱动程序供应商发布了一个修复程序。他们无法追踪问题:
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.
我已经 PHP 通过 ODBC 和 EasySoft 驱动程序与 DB2(Ubuntu 14.04 上的 v10.5)通信。我在使用 select id from new table(insert ....)
格式进行查询时遇到时间戳格式问题。
这是我的 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();
效果很好,原始 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();
真是奇怪。如果我尝试从 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_at
和 updated_at
的顺序在失败的情况下颠倒了:
...(insert into permissions (..., updated_at, created_at)...
一个愚蠢的问题,但是如果你修改顺序会怎样?
驱动程序供应商发布了一个修复程序。他们无法追踪问题:
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.