douchensou6495 2014-02-05 18:08
浏览 27
已采纳

MySQLi DATE_FORMAT根本不工作

I have the following code, the database column has got the date and time, I want to search for records using just the data part. I have the following but I have a feeling it is very wrong.

$query = "SELECT id,uid,product,jpgID,saledatetime FROM sales WHERE printed = ? AND DATE_FORMAT('?', 'y/m/d') AS saledatetime";

Edit: Working now, thanks:

if(login_check($mysqli) == true) {
    //logged in
    $session_date = $_SESSION['session_date'];

    $query = "SELECT id,uid,product,jpgID,saledatetime 
                FROM sales 
             WHERE printed = ? 
                AND DATE_FORMAT(saledatetime , '%y/%m/%d') = ?";
    if ($stmt = $mysqli->prepare($query)) {
        $printed = 0;
        $stmt->bind_param("is",$printed,$session_date);
        $stmt->execute();
        $stmt->store_result();

        //Get result
        $stmt->bind_result($result_id,$result_uid,$result_product,$result_jpgID,$result_saledatetime);

        //Number of rows returned
        $count_rows = $stmt->num_rows;
        echo $count_rows;
    }
} else {
    header("Location: index.php");  
}
  • 写回答

1条回答 默认 最新

  • dongmacheng3222 2014-02-05 18:17
    关注

    DATE_FORMAT has special syntax. Query didn't compare formatted datetime to anything, so I presume it was meant to be like this?

    SELECT id, uid, product, jpgID, saledatetime 
      FROM sales
     WHERE printed = ?
       AND DATE_FORMAT(saledatetime, '%y/%m/%d') = DATE_FORMAT(?, '%y/%m/%d')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路