2014-11-05 68 views
0

我想要計算用戶在Codeignter中獲得的通知量。我正在比較每個用戶行中保存的時間戳記,這些記錄是上次檢查通知頁面時記錄的時間戳記,以及每個通知添加的時間。但是我得到一個數據庫錯誤:比較兩個時間戳Codigniter

A Database Error Occurred 

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10:17:00' at line 5 

SELECT * FROM (`notifications`) WHERE `user_id` = '1' AND `instigator_id` != '1' AND added <= 2014-11-04 10:17:00 

文件名:/Users/true/Documents/Site/models/private/notifications_model.php

行號:152

通知控制器看起來像這

$data["user"] = $CI->Profile_model->get_public_profile_id($logged_user); 
$count = $CI->notifications_model->get_notifications_count($data["user"][0]["notes_check"]); 

型號:

public function get_notifications_count($time){ 


    $this->db->from($this->notifications_table); 
    $this->db->where('user_id', $this->session->userdata("id", 0)); 
    $this->db->where('instigator_id !=', $this->session->userdata("id", 0)); 
    $this->db->where('added <= '.$time, '', false); 
    $query = $this->db->get(); 
    $results = $query->result_array(); 

    return count($results); 



} 
+0

字符串需要封裝在引號中,總是 – MonkeyZeus 2014-11-05 21:55:15

+0

@MonkeyZeusTried with $ this-> db-> where('added <='。「$ time」,'',false);並仍然出現錯誤 – Matt 2014-11-05 21:57:04

+0

您是否閱讀過文檔? '$ this-> db-> where('some_field','some value');' – MonkeyZeus 2014-11-05 22:00:02

回答

0

您的日期/時間值是字符串。既然你沒有他們周圍' -quotes,MySQL是看到的是:

added <= 2014-11-04 10:17:00 
     ^^^^^^^^^^---mathematical subtraction = 1999 
        ^^^^^^^^^--illegal unknown field/table name 

應該

added <= '2014-11-04 10:17:00' 

注意引號。