【问题标题】:PHP Variable Not Being Executed In Mysql Query在 Mysql 查询中未执行 PHP 变量
【发布时间】:2015-10-07 13:27:27
【问题描述】:

我有一个 DateTime,我创建了这个月和上个月的两个变量,使用 format('m') 来获取当月的数字。然后我将其放入 mysql 查询中,但在使用变量时查询不起作用,但如果我手动输入数字则起作用。我可以调用该变量,它会回显两个正确的日期,所以我有点不知道为什么它在 mysql 查询中不起作用。

有什么想法吗?

谢谢

    $lastmonth = new DateTime();                                        
    $lastmonth->modify('-1 month');
    $lastmonth = $lastmonth->format('m');
    $thismonth = new DateTime();
    $thismonth = $thismonth->format('m');
    $thedate = "BETWEEN '2015-$lastmonth-16' AND '2015-$thismonth-15'";
    $query = ("SELECT unid FROM responses WHERE (date $thedate)");

    $stmt =  $db->prepare( $query );
    $stmt->execute();

我也试过了:

    $lastmonth = new DateTime();                                        
    $lastmonth->modify('-1 month');
    $lastmonth = $lastmonth->format('m');
    $thismonth = new DateTime();
    $thismonth = $thismonth->format('m');
    $query = ("SELECT unid FROM responses WHERE (date BETWEEN '2015-$lastmonth-16' AND '2015-$thismonth-15')");

    $stmt =  $db->prepare( $query );
    $stmt->execute();

【问题讨论】:

    标签: php mysql variables datetime


    【解决方案1】:

    你可以像这样使用:

        $lastmonth = new DateTime();                                        
        $lastmonth->modify('-1 month');
        $lastmonth = $lastmonth->format('m');
        $thismonth = new DateTime();
        $thismonth = $thismonth->format('m');
        $thedate = "BETWEEN '2015-".$lastmonth."-16' AND '2015-$thismonth-15'";
        $query = ("SELECT unid FROM responses WHERE (date ".$thedate.")");
    
        $stmt =  $db->prepare( $query );
        $stmt->execute();
    

    【讨论】:

    • 然后打印查询
    • 谢谢,我最初是这样做的,但忘了把它放在 mysql 中的 $thedate 变量周围。 :)
    • 好的,我的回答有帮助吗?
    • 又一个没有适当解释的复制粘贴解决方案来源。
    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 2022-11-18
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多