2-Best-Ways-To-Check-if-Email-Already-Exists-Using-PHP

In this article we discuss 2 Ways To Check if Email already exists Using PHP, to do that you also have to read about MySQL queries and syntax. On source code, we will use insert and select query to validate whether the email already exists or not.

Here is the list of 2 ways where we Check if Email already exists on the database Using PHP and MySQL,

  • Set the email column as UNIQUE on the Database.
  • Validate using MySQL select query with WHERE clause.
2 Best Ways To Check if Email Already Exists Using PHP
2 Best Ways To Check if Email Already Exists Using PHP

Set the Email Column as UNIQUE on the Database

To check if email already exists in the database using PHP we can use the UNIQUE column method. Where we set the email column as a UNIQUE column then MySQL will check and maintain the uniqueness of that field.

If you want to know more about SQL UNIQUE Key constraint check here: https://www.w3schools.com/sql/sql_unique.asp

Set Email Column as UNIQUE on the Database
Set the Email Column as UNIQUE on the Database

Result:

If you try to insert the same email ID it gives you an error message, “Duplicate entry ‘mail@gmail.com’ for key ’email’“.

Here are some major things used by the above code to check if email already exists in PHP MySQLi,

  1. Set the “Email” column as unique on the Database.
  2. Use die(mysqli_error($con) MySQL error function with insert query to show the errors.

Validate Email using MySQL SELECT query with WHERE clause

On the above code, we compare the user entered email with Database email using WHERE clause. After that we use mysqli_num_rows() to check the query result has any rows of data or not if data comes it means email matched with DB email which shows email already exists on DB.

Here are 2 best ways to check the email duplicacy with the Database. If you have any issues with implementation please let me know in the comment.

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

3 thoughts on “2 Ways To Check if Email already exists Using PHP”

Leave a Reply

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