有效月份中的 Oracle 日期错误

Oracle Date Error ON IN valid Month

我的查询

SELECT POSSESSION.*,PLOT.*,SCHEME_BLOCK.BLOCK 
FROM POSSESSION, PLOT, SCHEME_BLOCK 
WHERE POSSESSION.PLOT_ID=PLOT.PLOT_ID AND PLOT.PLOT ='10' 
AND PLOT.BLOCK_ID = SCHEME_BLOCK.BLOCK_ID AND SCHEME_BLOCK.BLOCK_ID='3' 
AND POSSESSION.CREATED_ON between '1420066800' and '1443650400' 

我的 php 代码如下..

$query = "SELECT " . $_REQUEST['tb'] . ".*,PLOT.*,SCHEME_BLOCK.BLOCK 
FROM " . $_REQUEST['tb'] . ", PLOT, SCHEME_BLOCK WHERE " . $_REQUEST['tb'] . ".PLOT_ID=PLOT.PLOT_ID AND PLOT.PLOT ='" . $_REQUEST['ps'] . "' 
AND PLOT.BLOCK_ID = SCHEME_BLOCK.BLOCK_ID AND SCHEME_BLOCK.BLOCK_ID='" . $_REQUEST['bid'] . "' AND SCHEME_BLOCK.CREATED_ON between '".strtotime(date('d-M-Y', strtotime($_REQUEST['sdate'])))."' and '".strtotime(date('d-M-Y', strtotime($_REQUEST['edate'])))."'";

在插入日期时我正在这样做

$CREATED_ON=date("d-M-Y");

错误:

ORA-01843: not a valid month
01843. 00000 -  "not a valid month"
*Cause:    
*Action:

请帮帮我...我怎样才能正确...我正在编码 php

提前致谢

尝试重新表述您的查询:

$query = "SELECT " . $_REQUEST['tb'] . ".*,PLOT.*,SCHEME_BLOCK.BLOCK 
FROM " . $_REQUEST['tb'] . ", PLOT, SCHEME_BLOCK WHERE " . $_REQUEST['tb'] . ".PLOT_ID=PLOT.PLOT_ID AND PLOT.PLOT ='" . $_REQUEST['ps'] . "' 
AND PLOT.BLOCK_ID = SCHEME_BLOCK.BLOCK_ID AND SCHEME_BLOCK.BLOCK_ID='" . $_REQUEST['bid'] . "' AND TRUNC(SCHEME_BLOCK.CREATED_ON, 'DD/MM/YYYY') between " . "TO_DATE('" . date('d-m-Y', strtotime($_REQUEST['sdate'])) . "', 'DD/MM/YYYY') and TO_DATE('" . date('d-m-Y', strtotime($_REQUEST['edate'])) . "', 'DD/MM/YYYY')";

POSSESSION.CREATED_ON between '1420066800' and '1443650400'

当您在 single-quotes 中有值时,它是 string,因此 '1443650400' 不是 DATE。您必须使用 TO_DATE 和适当的 format model.

将其显式转换为 DATE

首先,这段代码看起来像是简单的复制粘贴,你并不真正理解:

strtotime(date('d-M-Y', strtotime($_REQUEST['sdate'])))

它实际上是这样做的:

  1. 取字符串
  2. 转换为时间戳
  3. 转换为字符串
  4. 再次转换为时间戳

无论如何,Oracle 不理解 DATE 列中的 Unix 时间戳。您必须使用 TO_DATE() 函数来创建一个正确的日期,当然,还必须使用准备好的语句来使其完全正常。这条线上的东西:

$query = "SELECT ......... 
   AND POSSESSION.CREATED_ON between TO_DATE(:created_from, 'YYYY-MM-DD') 
       and TO_DATE(:created_to, 'YYYY-MM-DD')";

... 参数数组如下所示:

$params = array(
    'created_from' => date('Y-m-d', $created_from_unix_timestamp),
    'created_to' => date('Y-m-d', $created_from_unix_timestamp),
);

...或者这个:

$params = array(
    'created_from' => $created_from_datetime_object->format('Y-m-d')
    'created_to' => $created_to_datetime_object->format('Y-m-d')
);

不过,您必须知道没有时间的日期默认为 00:00:00,因此 created_on 中的一行 2014-01-10 01:00:00 大于 2014-01-10

最后但同样重要的是,将原始外部输入注入您的代码,如:

"SELECT " . $_REQUEST['tb'] . "...

... 是一个被黑客攻击的电话。严重地。通常的借口("I'll fix it later"、"it's just for internal usage")只是借口而已。