MySQL

How to Check if Email Already Exists in Database using PHP

In this tutorial, we are going to see different methods to check if email already exists in a database using PHP. If you are a “newbie” you should know how to connect to a MySQL database before using the code below. You cannot check if an email already exists in the database if you are not connected to it.
 

Method 1: How to Check if Email Already Exists in Database using PHP + PDO

To check if a particular value exists in the database, all you need to do is run a SELECT query.
 

<?php
//email to check
$email = "[email protected]";
//prepare the statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE email=?");
//execute the statement
$stmt->execute([$email]); 
//fetch result
$user = $stmt->fetch();

if ($user) {
    // email exists
} else {
    // email does not exist
} 
?>

 

Method 2: How to Check if Email Already Exists in Database using PHP + MySQLi

To check if a particular value exists in the database, all you need to do is run a SELECT query.

<?php

$select = mysqli_query($conn, "SELECT * FROM users WHERE email = '".$_POST['email']."'");
if(mysqli_num_rows($select)) {
    exit('This email address is already used!');
}
?>
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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