CodeIgniter Forums

Full Version: Get a single value from a a database ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

[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;