MySQLi DATE_FORMAT根本不工作

时间:2014-02-05 18:08:57

标签: php datetime mysqli

我有以下代码,数据库列有日期和时间,我想只使用数据部分搜索记录。我有以下但我觉得这是非常错误的。

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

修改 现在工作,谢谢:

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 个答案:

答案 0 :(得分:1)

DATE_FORMAT有special syntax。查询没有将格式化的日期时间与任何内容进行比较,所以我认为它应该是这样的?

SELECT id, uid, product, jpgID, saledatetime 
  FROM sales
 WHERE printed = ?
   AND DATE_FORMAT(saledatetime, '%y/%m/%d') = DATE_FORMAT(?, '%y/%m/%d')