【问题标题】:PDO wrong date bindingPDO错误日期绑定
【发布时间】:2009-07-03 11:54:16
【问题描述】:

我正在通过 odbc 查询一个 mssql 2008 数据库并发现了一个奇怪的行为。

使用如下代码...

$datefrom = "2009-06-01";
$dateto = "2009-07-01";
$clientno = "01";
$sth = $db->prepare("select count(*) from tbl WHERE v_valid_from <= ? and valid_from <= ? and (v_invalid_from >= ?) and (valid_to >= ? or valid_to is null) and clientno = ?");
$sth->execute(array($datefrom, $datefrom, $dateto, $dateto, $clientno));

查询检索到 80 行,但预期为 1000 行。

查看分析器,服务器执行以下语句:

select count(*) from tbl 
WHERE v_valid_from <= '20090601 00:00:00.000' and valid_from <= '20090601 00:00:00.000' and 
(v_invalid_from >= '20090701 00:00:00.000') and (valid_to >= '20090701 00:00:00.000' or valid_to is null) and clientno = '01'

当我在日期中输入“-”时,查询会正确执行。

日期时间值似乎没有特殊绑定,所以我想知道为什么会发生这种情况。

会话设置如下:

set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language English
set dateformat ymd
set datefirst 1
set transaction isolation level read committed

【问题讨论】:

    标签: php sql-server pdo


    【解决方案1】:

    我没有足够的代表发表评论。如果在开头设置日期值时将“-”更改为“/”会发生什么?例如:

    $datefrom = "2009/06/01";
    $dateto = "2009/07/01";
    

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2014-12-31
      • 2013-10-24
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      相关资源
      最近更新 更多