Welcome Guest, Not a member yet? Register   Sign In
row_array question
#1

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?
Reply
#2

Check num_rows()?
Reply
#3

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();

Reply
#4

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

Blush
Reply




Theme © iAndrew 2016 - Forum software by © MyBB