dsq30861 2015-07-31 14:49
浏览 76
已采纳

使用pdo从mysql检索今天的日期数据

I have some data in my table which are [name][address][phone_number] and the date in this format 2015-10-14 14:37:38. I am using php PDO. How can I query out just today date from the table?

The following code is my code to query out result for the past 7 days which worked perfectly. However, whenever I replace it with 1 it doesn't work:

$query = $digital->query('SELECT * FROM sales WHERE `datetime` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW() ORDER BY sale_id DESC'); 

What I want is to be able to query out all today data inserted into database.

  • 写回答

2条回答 默认 最新

  • dsmvqp3124 2015-07-31 15:07
    关注

    You can use this, haven't tested.

    <?php
    $todaysDate = date('Y-m-d'); //if your date is stored in y-m-d format
            try
            {
                $s = $conn->query("SELECT * from sales where datetime LIKE '%$todaysDate%'");
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }
            $results = $s->fetch(PDO::FETCH_OBJ);
    
    ?>
    

    Your query results is now in $results variable.

    Extended version

        <?php
    try
            {
                $s = $conn->query("SELECT * from sales");
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }
    
        while($results = $s->fetch(PDO::FETCH_OBJ))
        {
            $date = explode(" ", $results->datetime);
            if($date[0] == date('Y-m-d'))
            {
                //write code here to display data
            }
        }
    
    ?>
    

    Make sure you replace all the columnNames and tablename. Edit :- Here's a sqlfiddle pertaining to my first solution. http://sqlfiddle.com/#!9/7c2f1/3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路