【问题标题】:Filter by time and date按时间和日期过滤
【发布时间】:2013-11-14 21:52:33
【问题描述】:

我正在尝试从 MySQL 数据库中过滤结果,但运气不佳。

用户将填写一份表格,其中包含开始日期和截止日期(例如,从 2013 年 1 月 11 日到 2013 年 11 月 14 日)

这是我过滤结果的代码:

$dbserver = "localhost";
$dbname = "nameofDB";
$dbusername = "username";
$dbpassword = "password";

$mysqli = new mysqli($dbserver, $dbusername, $dbpassword, $dbname);

$query = "SELECT * FROM transfer WHERE personID = 84587749 AND DATE(time) BETWEEN ? AND ?";

if($stmt = $mysqli->prepare($query)){

    /*
    Binds variables to prepared statement


    i    corresponding variable has type integer
    d    corresponding variable has type double
    s    corresponding variable has type string
    b    corresponding variable is a blob and will be sent in packets
    */

   $to = $_POST['to'];
   $from = $_POST['from'];

   $stmt->bind_param('ss', $from, $to);


   /* execute query */
   $stmt->execute();

   /* Get the result */
   $result = $stmt->get_result();

   while ($row = $result->fetch_assoc()) {
        // Configure this how you want to print out each row.
        echo 'Details: '.$row['details'].'<br>';
        echo 'Time: '.$row['time'].'<br>';
        echo 'Balance: '.$row['balance'].'<br>';
        echo '<br><br>';
   }

   /* free results */
   $stmt->free_result();

   /* close statement */
   $stmt->close();
}

/* close connection */
$mysqli->close();

这显示没有结果,有人可以帮忙吗?

【问题讨论】:

    标签: php mysql time timestamp


    【解决方案1】:

    您从 $_POST 获得的值是什么?除非它们与 mysql 标准日期/时间字符串完全对应,例如

     yyyy-mm-dd
    

    如果没有适当的翻译逻辑,您就不能直接将这些 $_POST 值填充到您的查询中。

    例如

    ... WHERE DATE(time) BETWEEN 'Jan 31' and 'Feb 28'
    

    不是一个有效的比较,但是

    ... WHERE DATE(TIME) BETWEEN '2013-01-31' and '2013-02-28' 
    

    有效。

    【讨论】:

    • @user2149630:你为什么一遍又一遍地问同一个问题?您已经知道如何转换日期格式。 stackoverflow.com/a/19987026/67332stackoverflow.com/q/19988027/67332
    • @Glavić - 因为似乎没有人知道如何从表单中得到它,这不可能吗?
    • 表单提交的内容并不重要。如果您将这些 from-a-form-dates 传递给 mysql,则必须符合 mysql 的日期/时间字符串标准。
    猜你喜欢
    • 2023-03-23
    • 2013-10-20
    • 2022-07-16
    • 2021-09-01
    • 2021-12-27
    • 1970-01-01
    • 2018-01-25
    • 2013-07-03
    • 2018-06-17
    相关资源
    最近更新 更多