2011-05-30 109 views
1
$query = DB::select('thing')->from('things')->where('thing', '=', 'something'); 

if ($other_thing) 
{ 
    $query->and_where('other_thing', '=', 'something else'); 
} 

$query->order_by('thing', 'ASC')->limit(10)->execute()->as_array(); 

foreach ($query as $row) 
{ 
    echo $row['thing']; 
}

問題是什麼?查詢生成器和條件語句

好:

echo $row['thing'] -> nothing. 
print_r($query) -> an object and not an array. 

我在做什麼錯?有人可以幫助我嗎?請!

謝謝!

回答

0

試試這個:

$result = $query->order_by('thing', 'ASC')->limit(10)->execute()->as_array(); 

foreach ($result as $row) 
{ 
    echo $row['thing']; 
} 
+0

謝謝您的回答!一切正常! – user557108 2011-05-30 09:56:41

0

要在上面的回答擴大,問題是執行功能實際上是返回的Database_Result一個實例。正如以上文章所指出的,您可以調用該對象上的各種功能,然後以各種格式返回數據(請參閱前面的鏈接以獲取可用功能的完整列表)

這提供了各種好處 - this page描述了它們全部