2012-02-23 25 views
5

在Zend框架中,如何檢查zend_db_select是否返回結果?如何檢查Zend select是否返回結果

$result = $this->fetchAll(); 

有沒有更好的辦法,而不是使用:

if(count($result) != 0){ 
    //result found! 
} 
+3

你在這裏展示的方法有什麼問題? – 2012-02-23 07:43:41

+0

我在找一個更好的方法。 – rjmcb 2013-07-03 07:23:19

回答

9
$rows = $this->fetchAll(); 
return (!empty($rows)) ? $rows : null; 
6

我喜歡用經典:

//most of these queries return either an object (Rowset or Row) or FALSE 
    if (!$result){ 
     //do some stuff 
    } else { 
     return $result; 
    } 
1

方法返回NULL,而不是FALSE。使用if條件檢查此值。

2

我發現這種方式爲我工作得很好:

if($result->count() > 0) { 
    //Do something 
} 

感謝Åsmund

相關問題