简体   繁体   English

oci_execute():ORA-01840:输入值的长度不足以表示日期格式

[英]oci_execute(): ORA-01840: input value not long enough for date format

I am currently receiving the following error message from oci in php (I've been on it for a few hours). 我目前正在php中从oci收到以下错误消息(我已经处理了几个小时)。

oci_execute(): ORA-01840: input value not long enough for date format

It strange, because when I run the query within SQL Developer it seems to work fine. 这很奇怪,因为当我在SQL Developer中运行查询时,它似乎可以正常工作。

This makes me think that when I bind the parameter it is turning the dates into a type that is not able to calculate using conventional operators in oracle. 这使我认为,当我绑定参数时,它将日期转换为无法使用oracle中的常规运算符进行计算的类型。

$startDateTime = '2015-03-06 00:00:00';
$endDateTime = '2015-04-06 00:00:00';
$value = '20';
$type = '$';

$SQL = "SELECT count(*) AS \"COUNT\"
               FROM bonus where value = :d_value
               AND TYPE = :d_type
               AND ((:d_valid_from between valid_from AND valid_till) OR (:d_value_till between valid_from AND valid_till) OR (:d_valid_from < valid_from AND valid_till < :d_valid_till))";

 $this->stmnt = $this->conn->prepare($SQL);
 $this->stmnt->bindParam('d_valid_from', $startDateTime);
 $this->stmnt->bindParam('d_valid_till', $endDateTime);
 $this->stmnt->bindParam('d_value', $value);
 $this->stmnt->bindParam('d_type', $type);
 $this->stmnt->execute();

I am unable to find many resources that deal with php directly with this problem in hand. 我无法找到许多直接解决此问题的PHP资源。 Does anybody have any experience with it? 有人有经验吗?

I think that your dates are being bound as strings in the query. 我认为您的日期被绑定为查询中的字符串。 Assuming that the columns you are comparing it to (eg valid_from ) are dates, then the string value is being converted to a date using the default date format for the sessions. 假设您要与之进行比较的列(例如valid_from )是日期,则使用会话的默认日期格式将字符串值转换为日期。 The difference in behavior in SQL Developer is probably because the default format is different. SQL Developer在行为上的差异可能是因为默认格式不同。

Anyway, the solution is to follow a very simple and important rule, which is not to rely on default type conversion. 无论如何,解决方案是遵循一个非常简单且重要的规则,即不依赖默认类型转换。 Explicitly convert the string to a date in your query, specifying the appropriate format: 将字符串明确转换为查询中的日期,并指定适当的格式:

TO_DATE( :d_valid_from, 'YYYY-MM-DD HH24:MI:SS' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM