Welcome Guest, Not a member yet? Register   Sign In
How can i get single value without foreach
#1

[eluser]Zeeshan Rasool[/eluser]
Hi guys, here im trying to fetch single value from db
is there any method that get single value without foreach

Code:
foreach ($query as $item){
echo $item->res_article_title ;
}
how can i get rid of this...?

thnx in adv.
#2

[eluser]Doosje[/eluser]
Code:
$row = $query->row();
echo $row->a_field_name;
#3

[eluser]sophistry[/eluser]
a refinement on Doosje's suggestion (which is good) is to also use the LIMIT parameter in your query.

check the db docs, i think it is the second param you can send with a $this->db->get() method. that way you only get one record.
#4

[eluser]onejaguar[/eluser]
I created a helper function to do this for me:

Code:
//
// Return database results with a single value
//
function db_pop(&$result) {
  if ($result->num_rows() == 1)
    return array_pop($result->row_array());
  return null;
}

just call with

Code:
echo db_pop($query);




Theme © iAndrew 2016 - Forum software by © MyBB