I’m having this dillemma in CodeIgniter, for what is the most error-prone way to check for results. Let’s asume I have this snippet of code:
function getByUsername($username){
$this->db->where('username',$username);
$query=$this->db->get('user');
if($query->num_rows() == 1)
{
return $query;
}
}
Well, you can check ‘num_rows()’ here, or you could check it in your controller or view. For me the best place to check it obviously seems in the model. But if you do that, you have to check it somewhere else again if it even contains anything.
I am kind of a PHP n00b, so please help me out, I’m probably missing the easiest solution there is, but I can’t see it.
there’s never a definitive answer as MVC is only a concept, and both will work fine in practice.
However generally this is best done in the Model. You can return the result on success, or return false on failure.
Your controller can handle the logic ~
You may get a bunch of opinions on this and none of them will be right or wrong.
edit
so in you example:
then your controller can be: