CodeIgniter Forums
row_array question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: row_array question (/thread-69023.html)



row_array question - muuucho - 09-28-2017

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?


RE: row_array question - Narf - 09-28-2017

Check num_rows()?


RE: row_array question - dave friend - 09-28-2017

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




RE: row_array question - muuucho - 09-28-2017

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

Blush