2009-07-03 26 views
0

我通過odbc查詢一個mssql 2008數據庫,並且遇到了奇怪的行爲。PDO錯誤的日期綁定

下面的代碼是用來...

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

雖然1000預計查詢檢索80行。

展望剖析,服務器執行以下語句:

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 

回答

0

我沒有足夠的代表處發表評論。如果在開始時設置日期值時將「 - 」更改爲「/」會發生什麼?例如:

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