How to check unique username and email in php.

02nd, January 2018 By

To find unique username and email entered while registration in php You have to fetch record from database based on the username entered in form using $_POST[‘username’] . I have shown how you can check for username and email in step1 and step2 examples respectively.

Step1

You have to make query for users table that will give result if already in the table see example for username :

<?php $sql="select username from users where username='".$_POST['username']."'";
 $result=mysql_query($sql) or die (mysql_error().$sql); 
$count=mysql_num_rows($result);
if ($count>0) {
return false;
} else {
return true;
}
?>

Here you can see the logic if count will be grater than zero it means record already exists and it will return false otherwise it will return true.

Step 2

For email you also need to check for the email if there is in the users table based on that the condition will be made see example for email:

<?php $results = mysql_query("select * from users where email = '".$_POST['email']."' $where ")  or die(mysql_error());
 $result = mysql_num_rows($results);
if($result> 0){
return false;
} else {
return true;
}
?>

The only difference is that here we used email in place of username if email already exists than result variable become greater than zero and it will return false but if it does not match with any email record then it will return true.

Leave a Reply

Your email address will not be published. Required fields are marked *