Query Builder and Conditional Statements
$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'];
}
And what the problem is?
Well:
echo $row['thing'] -> nothing.
print_r($query) -> an object and not an array.
What am I doing wrong? Can someone help me on this? Please!
Thank you!
try this:
$result = $query->order_by('thing', 'ASC')->limit(10)->execute()->as_array();
foreach ($result as $row)
{
echo $row['thing'];
}
To expand on the above answer, the problem is that the execute function is actually returning an instance of Database_Result. As the above post points out you are able to call various functions on this object that will then return the data in various formats (see the previous link for a full list of available functions)
This provides various benefits - this page describes them all
精彩评论