0

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

用户将填写一份表格,其中包含从和到日期(EG FROM 01/11/2013 TO 14/11/2013)

这是我过滤结果的代码:

$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();

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

4

1 回答 1

0

您从 $_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' 

有效的。

于 2013-11-14T21:55:04.110 回答