Codeigniter email Validar e DuBlicate do banco de dados no PHP

// THIS HELPER METHOD CHECKS IF THE EMAIL IS VALID OR NOT. IT BASICALLY CHECKES THE DUPLICATION
function email_duplication($email = "", $user_id = "")
	{
		$CI	= &get_instance();
		$CI->load->database();

		if (filter_var($email, FILTER_VALIDATE_EMAIL)) { //FILTER MAIL FIRST
			
			$query = $CI->db->get_where('users', ['email' => $email]);
			if (!empty($user_id)) { //IF USER-ID EXIST
				$query_result = $query->row_array();
				if ($query->num_rows() == 0 || $query_result['id'] == $user_id) { //IF NOT FIND ANY DUBLICATE
					return true;
				} else { //IF FIND
					$CI->session->set_flashdata('notice_message', get_phrase('Email_already_exists'));
					redirect($_SERVER['HTTP_REFERER'], 'refresh');
				}
			} else { //WHEN USER ID NOT EXIST
				if ($query->num_rows() > 0) { //FIND DUBLICATE
					$CI->session->set_flashdata('notice_message', get_phrase('Email_already_exists'));
					redirect($_SERVER['HTTP_REFERER'], 'refresh');
				} else { //NOT FIND
					return true;
				}
			}

		} else { //INVALID EMAIL
			$CI->session->set_flashdata('error_message', get_phrase('Invalid_email'));
			redirect($_SERVER['HTTP_REFERER'], 'refresh');
		}
		

	}
Codelone