1

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;
            }
        }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Mehur
  • 131
  • 1
  • 3
  • 11
  • you `select ... where email=$email or username=$username`, basically. since you've provided absolutely NO details as to what this `db->where` stuff is, or how it works, that's about all we can suggest. – Marc B Oct 12 '16 at 19:46
  • Sorry i am using codeigniter https://www.codeigniter.com/ @MarcB – Mehur Oct 12 '16 at 19:48
  • This certainly is possible, but does not really make too much sense. It is a more effective strategy to simply try to insert right away. That will fail if your combined index, declared as unique, already contains the combination. That is enough for you to conclude an already taken user name. This saves you the additional select statement, thus saves time and code. – arkascha Oct 12 '16 at 19:48

3 Answers3

1

There are 3 approaches. Method 1, do 2 queries, first one to check if it already exists and the 2nd query to do the insert if the 1st found zero matches.

Method 2, use insert ignore syntax, so duplicate inserts silently fail.

Method 3, use ON DUPLICATE KEY UPDATE syntax to update columns of a entry that would have been inserted if it didn't already exist.

Duane Lortie
  • 1,285
  • 1
  • 12
  • 16
1

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;
            }
        }
  • Hello i want to know, how to check if someone post email address as username and how to make them valid? – Mehur Oct 12 '16 at 21:24
  • If user post email or username through same `$username` then how to check that is valid or exist ? – Mehur Oct 12 '16 at 21:30
  • Can you provide two examples of valid values for `$username` I am assuming you mean that when the function is called you can call the function as `check_all('Mehur')` or `check_all('Mehur@place.com')` and that you would like the function to return if the user exist? If you can provide more detail in your question so we can better answer it, and provide examples. – ebellefontaine Oct 12 '16 at 21:42
  • Please check http://stackoverflow.com/questions/40008992/how-to-check-email-or-username-is-valid-or-exist-through-same-post – Mehur Oct 12 '16 at 21:53
  • Any alternative of `EMAIL IS NOT NULL` ? I have `email` in row so should i use `->where('email')` ? – Mehur Oct 12 '16 at 22:22
0

Just give validation it's simple and easy for checking already exists user / email in database

[
'field' => 'username',
'label' => 'User Name',
'rules' =>'trim|required|is_unique[login.username],
                        'errors' =>array('is_unique' => 'The %s is already  taken')                                  

]

Here, is_unique[login.username] where login is table name and username is column which directlt see in database and check for unique value

for more go to CI

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jalpesh
  • 63
  • 6