dealing with null values from a database
I have a function that pulls users details out of a database.
function getUser($user_id){
$this->db->from('users');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
$result = $query->result();
return $result[0]; 开发者_如何学编程
}
However if the user has deleted their account then this gives me the following error = "Undefined offset: 0".
This causes me a few issues as users can add each other as friends and the "getUser" function is used to pull out their friends details. However if (as mentioned) the friend has deleated their account it causes me a few headakes.
Is there anyway to filter this function of null values?
Cheers!!
return (isset($result[0])) ? $result[0] : null;
精彩评论