按时间和日期过滤

时间:2013-11-14 21:52:34

标签: php mysql time timestamp

我正在尝试从MySQL数据库中过滤结果,但没有太多运气。

用户将填写一份表格,其中包含起始日期(EG FROM 01/11/2013至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();

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

1 个答案:

答案 0 :(得分:0)

你从$ _POST获得的价值是多少?除非它们完全对应于mysql标准日期/时间字符串,例如

 yyyy-mm-dd

然后,如果没有适当的翻译逻辑,您无法直接将这些$ _POSt值填充到查询中。

e.g。

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

不是有效的比较,但是

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

IS 有效。