2014-04-16 76 views
0

在我的應用程序中,我想打印一份列表,列出最近5分鐘在線的成員。帶間隔的ZF2 mysql查詢

我試圖通過下面的代碼與MySQL間隔創建此;

$interval = 5; 
    $select->where(array('member_last_online > ?' => 'SUBDATE(NOW(), INTERVAL '.$interval.' MINUTE)')); 

哪些產生此通知;

Notice: Attempting to quote a value without specific driver level support can introduce security vulnerabilities in a production environment. in ..zf_path.. 

'getSqlSTring()'的輸出是;

SELECT "members".* FROM "members" WHERE member_last_online > 'SUBDATE(NOW(), INTERVAL 5 MINUTE)' 

如果我下面直接放在桌子上執行查詢返回的數據爲接受:

SELECT members.* FROM members WHERE member_last_online > SUBDATE(NOW(), INTERVAL 5 MINUTE) 

什麼是我錯在這裏做什麼?

+0

我認爲你需要做一些事情:'$ adapter-> platform-> quoteIdentifier($ interv al)'看看這裏:http://stackoverflow.com/questions/13287731/quote-value-into-zend-framework-2 – Uffo

回答

1

您需要將數據庫平臺傳遞給getSqlString(),以避免該警告(所以它知道如何報價值):

// assuming $db is the DB adapter instance 
echo $select->getSqlString($db->getPlatform()); 

解決您的主要問題是:

$select->where(new \Zend\Db\Sql\Predicate\Expression('SUBDATE(NOW(), INTERVAL '.$interval.' MINUTE)')); 

或更易讀:

use Zend\Db\Sql\Predicate\Expression; 

$select->where(new Expression('SUBDATE(NOW(), INTERVAL '.$interval.' MINUTE)')); 
+0

在我的情況,'echo $ select-> getSqlString($ this-> getAdapter( ) - > getPlatform())'努力消除警告:) – webcoder