sql query for login with email or username 1

sql query for login with email or username

if($_POST['username'] != '' && $_POST['password'] != ''){$name = $_POST['username'];$pass = md5($_POST['password']);$check_email = Is_email($name);if($check_email){// email & password combination$query = mysql_query("SELECT * FROM `users` WHERE `email` = '$name' AND `password` = '$pass'");} else {// username & password combination$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$name' AND `password` = '$pass'");}$rows = mysql_num_rows($query);if($rows > 0){//successfull login$_SESSION['username'] = $name;} else {$msg = "Invalid Login Credentials";}mysql_close($connection);} else {$msg = "Please Provide All Details";}

Here is what the above code is Doing:
1. It checks if the username and password fields are not empty.
2. It checks if the username is an email address.
3. If it is an email address, it checks the database for the email and password combination.
4. If it is not an email address, it checks the database for the username and password combination.
5. If the combination is found, it sets the username in the session.
6. If the combination is not found, it sets the error message.

Similar Posts