2013-05-01 108 views
0

我正在使用Zend的便捷方法進行數據庫調用fetchAll。正如知識淵博的人應該知道的那樣,這是一個允許查詢參數化的功能。例如,查詢可能是:參數化MySQL查詢IN子句

$query = "select * from user where email = ?" 
$results = $this->_db->fetchAll($query, $email); 

而這就是如何實現參數化。

我查詢然而,就是這樣的:

$query = "select * from user where email in ("[email protected]", "[email protected]","[email protected]"); 

我怎麼能參數上面的查詢,因爲這些電子郵件是用戶輸入的,所以我不會簡單地讓它們在原始查詢和嘗試失敗後:

$query = "select * from user where email in ? "; 
$this->_db->fetchAll($query, $commaSeparatedEmailList); 

其中$commaSeparatedEmailList = "(".implode("," , $emails).")";

任何想法?

回答

2

不幸的是ZF對於如何處理這個問題有點不一致。有些類可以處理數組參數,但不幸的是,適配器類上的fetchAll()不是其中的一個。有兩種(略帶凌亂)選擇我所知道的:

切換到構建查詢與Zend_Db_Select,而不是(這不正確處理它):

$select = $db->select(); 
$select->from('user') 
     ->where('email IN (?)', $email); 

$db->fetchAll($select); 

或堅持fetchAll和使用quoteInto:

$db->fetchAll("SELECT * FROM user WHERE ".$db->quoteInto('email IN (?)', $email));