简体   繁体   English

日期格式在使用Informix的PHP查询中失败(基于SQL)

[英]Date format is failing in php query using informix (SQL-based)

I'm running a service which (should) returns a json with a response from a sql query to fill a table. 我正在运行一项服务,该服务(应该)返回json以及来自sql查询的响应以填充表格。 For debugging purposes i've added the resulting query string to the response to try it directly on the database and the query works in my database. 出于调试目的,我已将结果查询字符串添加到响应中,以便直接在数据库上进行尝试,并且该查询在我的数据库中有效。 Actually im getting the following errors when i run it from my service and i need to solve this: 当我从服务运行它时,实际上我收到以下错误,我需要解决此问题:

Invalid year in date. 无效的年份。 When i go for "and table.datefield >= '".$date."' 当我去“ and table.datefield> ='”。$ date。“'

Invalid month in date. 日期中的月份无效。 When using "and table.datefield >= {d'".$date."'} 使用“ and table.datefield> = {d'”。$ date。“'}时

Both with HY000 error code. 两者都带有HY000错误代码。 (Description below) (以下说明)

SQLSTATE[HY000]: General error: -1204 [Informix][Informix ODBC Driver][Informix]Invalid year in date (SQLPrepare[-1204] at /usr/PRODUCTO/apache_jano/PDO_INFORMIX-1.3.3/informix_driver.c:131) ISAM: 264 SQLSTATE [HY000]:常规错误:-1204 [Informix] [Informix ODBC驱动程序] [Informix]无效的年份日期(SQLPrepare [-1204]位于/usr/PRODUCTO/apache_jano/PDO_INFORMIX-1.3.3/informix_driver.c:131 )的ISAM:264

SQLSTATE[HY000]: General error: -1205 [Informix][Informix ODBC Driver][Informix]Invalid month in date (SQLPrepare[-1205] at /usr/PRODUCTO/apache_jano/PDO_INFORMIX-1.3.3/informix_driver.c:131) ISAM: 264 SQLSTATE [HY000]:常规错误:-1205 [Informix] [Informix ODBC驱动程序] [Informix]日期中的月份无效(SQLPrepare [-1205]位于/usr/PRODUCTO/apache_jano/PDO_INFORMIX-1.3.3/informix_driver.c:131 )的ISAM:264

At some point the 2nd option from above worked (it detects the year so i think its the closest approach), but it doesnt anymore. 在某些时候,上面的第二个选项起作用了(它检测到年份,所以我认为它是最接近的方法),但现在不再有效。 I can't either find the place where i found the {d } documentation so (which solved it in the past), any link would also be appreciated. 我或者找不到我找到{d}文档的地方,所以(过去解决了它),任何链接也将不胜感激。 One more thing i cant use bind_params for this (I've been explicitly told to do it without this). 我不能为此使用bind_params(我已被明确告知要在没有此情况下执行此操作)。

This is part which is failing (deleting this condition in my query works perfect) 这是失败的部分(在我的查询中删除此条件非常有效)

SQL code condition which works in dbeaver and taken from response string: 在dbeaver中有效并从响应字符串获取的SQL代码条件:

 AND (

(table.fecha_inicial <= { d '2018-07-15' }AND (table.fecha_final >= { d '2019-07-15' }OR table.fecha_final IS NULL))

OR 

(table.fecha_inicial >= { d '2018-07-15' }AND (table.fecha_final <= { d '2019-07-15' } OR table.fecha_final IS NULL)))

PHP coding PHP编码

    if ($fechaInicio){ $fechaInicio = date('Y-m-d',     strtotime($fechaInicio));}
    if ($fechaFin){ $fechaFin = date('Y-m-d', strtotime($fechaFin));}
    $cond_fechas = " AND ( (table.fecha_inicial     <= '".$fechaInicio."' AND (table.fecha_final >= {d'".$fechaFin."'}     OR table.fecha_final IS NULL)) "
                    . " OR (table.fecha_inicial     >= '".$fechaInicio."' AND (table.fecha_final <= {d'".$fechaFin."'}     OR table.fecha_final IS NULL)))";


 $result['query'] = $query;
 $conn = DB::getConnection('bbdd');
 $result['das']= $conn->preparedQuery($query);

if (isset($result['das'])) {
   return JwtCheckHeader::json($result['das']);
} else {
   return JwtCheckHeader::json($result);
}

Ok i found the issue here. 好的,我在这里找到了问题。 It should return some error of "no results matching your conditions" instead of the "format_error" which mislead me completely. 它应该返回“没有符合条件的结果”错误,而不是完全误导我的“ format_error”错误。

This error happened when comparing some of the db empty date strings in database with the dates provided. 将数据库中的某些db空日期字符串与提供的日期进行比较时,会发生此错误。

It fails when no data meets and success when the dates provided meet requirements, so it works! 如果没有数据满足,它将失败;提供的日期满足要求时,它将成功,因此可以正常工作! Hope it will help somebody else in the future. 希望将来对其他人有帮助。

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

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