Home » 2 Ways To Check if Email already exists Using PHP

2 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 use insert and select query to validate the email is already exists or not.

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

  • Set 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 Email Column as UNIQUE on the Database

To check if email already exists in database using PHP we can use the UNIQUE column method. Where we set the email column as 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 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 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 is 2 best ways to check the email duplicacy with Database. If you have any issue with implementation please let me know on the comment.

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

Advertisement

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

  1. Pingback: Allow Only String and Numbers Using PHP - PHPCODER.TECH

  2. Pingback: Image Creation From User Input in PHP - PHPCODER.TECH

Leave a Reply

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