【问题标题】:The Mysql query in PHP code doesn't workPHP代码中的Mysql查询不起作用
【发布时间】:2013-04-11 14:51:34
【问题描述】:

这是我只想在时间戳中的某个日期上方选择 "tbl_name,itime_end,itime_start" 的代码:

$reponse = $bdd->query('
SELECT 
tbl_name,
itime_end,
itime_start 
FROM table_ref 
WHERE `itime_start` > ".$Timestamp_UserStartDate." 
')

itime_start$Timestamp_UserStartDate 在时间戳中

但是当我执行 gettype 时,$timestamp_USerStartDate 是一个整数,$itime_start 是一个字符串。 echo $timestamp_USerStartDate; 给出:1365408000 echo $donnees['itime_start']; 给出:1364998028 ...

结果是所有选择的数据都显示出来,没有经过过滤! 提前感谢您的建议!

【问题讨论】:

  • 您确定单引号、双引号和连接点的顺序正确吗?
  • 是的,就是这样,tnx!

标签: php mysql string request timestamp


【解决方案1】:

首先,您真的不应该以您的方式注入变量。您应该绑定变量占位符,然后再填充它们。但是,您的代码的问题在于您没有以您想要的方式结束单括号。试试下面的行

$reponse = $bdd->query('SELECT tbl_name,itime_end,itime_start FROM table_ref WHERE `itime_start` > "'.$Timestamp_UserStartDate.'" ')

【讨论】:

  • 最后一个分号? ;)
  • 分号不是必须的
  • @Bryan Moyles 你知道如何将结果放入新查询的 SELECT 中,并为第一个查询的每个结果显示第二个查询的结果吗?
  • 我建议要么进行子选择,要么更优选地进行 JOIN。因此,在您的查询中,您可以执行 SELECT * FROM (SELECT ....) t,请注意 ) t 不是拼写错误,t 或某些任意字符来别名子选择是必要的
【解决方案2】:

如果您查看生成的查询,您会发现它不是您想要的。

$reponse = $bdd->query("
    SELECT 
        tbl_name,
        itime_end,
        itime_start 
    FROM table_ref 
    WHERE `itime_start` > " . $Timestamp_UserStartDate
);

只要itime_start$Timestamp_UserStartDate 都是 UNIX 时间戳,就可以满足您的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多