使用php时str_to_date查询返回错误的顺序

时间:2017-12-08 06:59:45

标签: php mysql datetime

我创建了一个在phpmyadmin上完美运行的查询

SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%d-%m-%y') as horo FROM userlogin WHERE type='user' ORDER BY horo  DESC

但当我在PHP上传递此查询时返回错误为query was empty 我的PHP代码是

$selectorders=sprintf("SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%d-%m-%y') as hor FROM userlogin WHERE type='user' ORDER BY horo  DESC");
$myorderquery = mysql_query($selectorders) or die(mysql_error());
$row_rsselect = mysql_fetch_assoc($myorderquery);

1 个答案:

答案 0 :(得分:2)

您无缘无故地使用sprintf。由于您的日期格式使用'%'如果你想要使用这个功能,你需要加倍它们:

$selectorders=sprintf("SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%%d-%%m-%%y') as hor FROM userlogin WHERE type='user' ORDER BY horo  DESC");

只需使用字符串声明即可。