CodeIgniter Forums
Get a single value from a a database ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Get a single value from a a database ? (/thread-27100.html)



Get a single value from a a database ? - El Forum - 02-01-2010

[eluser]chefnelone[/eluser]
Hello I have this funcion in a model.

I retrieve one value (product_name) for a table (products).
It works fine but... as you can see I'm using a foreaech() to echo the result.

Since I just get 1 result I don't want to use the foreach() statement.

Question: How do I echo the result NOT using foreach()

Code:
function get_product(){
  $this->db->where("product_id", "10");
  $this->db->limit(1);
  $query = $this->db->get('products');

  foreach ($query->result() as $row)
    {
     echo $row->product_name;
    }    
}

Thanks


Get a single value from a a database ? - El Forum - 02-01-2010

[eluser]n0xie[/eluser]
Code:
$this->db->select('product_name');
  $this->db->where("product_id", "10");
  $this->db->limit(1);
  $query = $this->db->get('products');
  
  echo $query->row()->product_name;