Welcome Guest, Not a member yet? Register   Sign In
Should i use num_rows() to test first?
#1

[eluser]Unknown[/eluser]
Hello,

Code:
public function getSlug($where= array()) {
        $query = $this->db->get_where('tablename', $where);
        return $query->row();
    }


when i call getSlug by value, should i put num_rows() in this function?
sth like:
Code:
public function getSlug($where= array()) {
        $query = $this->db->get_where('tablename', $where);
if ($query->num_rows() > 0)
        return $query->row();
    }
return 0;

how about using php count() function instead of num_rows() , which is better?
sth like:
Code:
public function getSlug($where= array()) {
        $query = $this->db->get_where('tablename', $where);
        return $query->row();
    }


if(count($this->model->getSlug())){
echo 'got it';
}else{
echo 'nth' ;
}


sry for my poor english. thanks all for help



#2

[eluser]CroNiX[/eluser]
That's up to you and how you need your data, which could vary from instance to instance.

Regarding num_rows() vs native count(), num_rows() will always be faster and cheaper because it's already calculated as part of the query result, while count() would have to basically redo what num_rows() already did, which will take longer and consume more memory.

So I would do this, except return null if nothing was found, since 0 could be a legitimate value in some query results.
Code:
if ($query->num_rows() > 0)
        return $query->row();
    }
return 0;




Theme © iAndrew 2016 - Forum software by © MyBB