CodeIgniter Forums

Full Version: row_array question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this function in a model_
Code:
function get_one($where_clauses)
   {
       return $this->db->get_where('recipes', $where_clauses)->row_array();
   }
If I, by mistake, pas in where_clauses that returns more than one row only the first row is returned since I am using row_array().
However, is there a way to get a warning (like return false) if more than one row is fetched?

I could use result_array and count the rows, but then I get multidimensional array in return.
Any better solution?
Check num_rows()?
Expanding on Narf's correct but very short reply

PHP Code:
function get_one($where_clauses)
{
 
      $query $this->db->get_where('recipes'$where_clauses);
 
      if($query->num_rows() > 1)
 
      {
 
          return FALSE;
 
      }
 
      return $query->row_array();


Or, using a ternary instead of an "if"

PHP Code:
function get_one($where_clauses)
{
 
      $query $this->db->get_where('recipes'$where_clauses);
 
      return $query->num_rows() > FALSE $query->row_array();

(09-28-2017, 05:52 AM)Narf Wrote: [ -> ]Check num_rows()?

Blush