doutong7216 2013-11-14 21:52
浏览 322
已采纳

按时间和日期过滤

I am trying to filter results from a MySQL DB but not having much luck.

The user will complete a form, with a from and to date (EG FROM 01/11/2013 TO 14/11/2013)

Here is my code to filter the results:

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

This is showing no results, can anyone help?

  • 写回答

1条回答 默认 最新

  • dtl19910708 2013-11-14 21:55
    关注

    What are the values you're getting from $_POST? Unless they correspond exactly to a mysql standard date/time string, e.g.

     yyyy-mm-dd
    

    Then you cannot directly stuff those $_POSt values into your query without appropriate translation logic.

    e.g.

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

    is not a valid comparison, but

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

    IS valid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条