Mehur Mehur - 1 month ago 7
MySQL Question

How to check posted value already exist in db or not?

I want to check if posted username is exist in db with email address or not.
How to check that?

public function check_all($username)
{
$check = $this->db->where("username","email", $username)->get("users");
if($check->num_rows() > 0) {
return false;
} else {
return true;
}
}

Answer

If you wanted to check if a username exist you could try the following code snippet

public function check_all($username) 
        {
            $results = $this->db->select('USERNAME')
                               ->from('USERS')
                               ->where('USERNAME', $username)
                               ->get();

            if($results->num_rows() > 0)
            {
              //The user exist
              return true;
            } else {
                return false;
            }
        }

If you're looking to check if the user has an email address you could do

public function check_all($username) 
        {
            $results = $this->db->select('USERNAME')
                                ->from('USERS')
                                ->where('USERNAME', $username)
                                ->where('EMAIL IS NOT NULL')
                                ->get();

            if($results->num_rows() > 0)
            {
              //The user has an email
              return true;
            } else {
                return false;
            }
        }
Comments