2016-12-29 26 views
-1

我有一個自定義的GC會話方法集應該刪除我的數據庫中的任何舊會話。這裏是FUNC:正確的PHP會話垃圾收集語法?

public function gc($max){ 
     // Calculate what is to be deemed old 
     date_default_timezone_set('America/Chicago'); 
     $old = time() - $max; 

     // Set query 
     $this->db->query('DELETE * FROM session WHERE access < :old'); 

     // Bind data 
     $this->db->bind(':old', $old); 

     // Attempt execution 
     if($this->db->execute()){ 
     // Return True 
     return true; 
     } 

     // Return False 
     return false; 
    } 

的$最大無功代表我的php.ini的session.gc_maxlifetime值。這裏有垃圾收集設置在我的.ini:

session.gc_probability = 1 
session.gc_divisor  = 100 
session.gc_maxlifetime = 1440 

但是每當這個功能運行時,我總是得到以下致命錯誤:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 '* FROM Sessions WHERE access < 1482965344' at line 1' in /Applications/MAMP/htdocs/demo/DB.php:66

不禁覺得我失去了一些東西瑣碎...有什麼建議?

+0

'在'*'附近使用正確的語法它告訴你錯誤在哪裏開始;您的查詢無效。 –

+0

你在另一個問題中使用了正確的語法http://stackoverflow.com/q/39928440/1415724'刪除會話'所以你爲什麼現在要做'DELETE *'? –

回答

0

您的SQL查詢錯誤。 DELETE和FROM之間沒有*。只需使用下面這行:

$this->db->query('DELETE FROM session WHERE access < :old'); 
1

你應該使用:

'DELETE FROM session WHERE access < :old'爲您的DML字符串。

刪除*