How to check if email exists in the database?
A common mistake
A very common newbie mistake is to check the result of the query, like
$check_email = $pdo->query("SELECT * FROM users WHERE email='test@example.com'");
if ($check_email) {
// found
}
Alas, it's entirely wrong. The result of the query has nothing to do with the number of rows found. An empty result is still a legitimate result, it cannot be false only because the query didn't find anything.
In fact, the code to check whether some value exists in the database would be the just a regular routine for select queries.
And of course, a prepared statement is obligatory. NEver ever put a data variable directly tho the query. Always use a placeholder instead!
How to check whether a value exists?
To check whether a particular value exists in the database, you simply have to run just a regular SELECT query, fetch a row and see whether anything has been fetched.
Given $pdo contains a valid PDO instance, the code would be
$email = "test@example.com";
$stmt = $pdo->prepare("SELECT * FROM users WHERE email=?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user) {
// email found
} else {
// or not
}
Here we are selecting a row matching our criteria, then fetching it and then checking whether anything has been selected or not.
Notes
It also makes sense to select just a single constant value to reduce overhead a little, if we aren't going to use any selected data. For that purpose simple replace * with 1. Having a function or a class' method for the purpose also would be handy:
function emailExists($pdo, $email) {
$stmt = $pdo->prepare("SELECT 1 FROM users WHERE email=?");
$stmt->execute([$email]);
return $stmt->fetchColumn();
)
if (emailExists($pdo, $email)) {
// found
}
here we are using a handy PDO's method fetchColumn that returns a selected value or false otherwise. Given 1 will be treated as true by if statement, we can return this method's result directly. However, in case you prefer strict typing, the return value could be cast to boolean:
return (bool)$stmt->fetchColumn();
Also, as can be seen from above, a popular solution with getting the number of rows returned is just unnecessary. Calling $stmt->numRows() is just superfluous, not to mention it can be just unavailable due to some drivers or settings.
Related articles:
- SELECT query with PDO
- INSERT query using PDO
- UPDATE query using PDO
- How to connect to MySQL using PDO
- PDO Examples
- Authenticating a user using PDO and password_verify()
- Select the number of rows using PDO
- How to create a WHERE clause for PDO dynamically
- DELETE query using PDO
- How to create a prepared statement for UPDATE query
- Getting a nested array when multiple rows are linked to a single entry
- How to execute 1000s INSERT/UPDATE queries with PDO?
- Adding a field name in the ORDER BY clause based on the user's choice
- INSERT helper function for PDO Mysql
- PDO Examples
- PDO Examples
Comments:
If you only want to know whether a row exists or not, but don't care about how many rows, always add LIMIT 1 to the query. This is more efficient since the query can stop processing as soon as it has found one row. So for example: SELECT 1 FROM users WHERE email=? LIMIT 1
If l put my email ID showing that 'your email id not in our data base
I wnat to make a dynamic multiforms stepper using php and mysql Email id - niteshvishwakarma099@gmail.com
I wnat to make a dynamic multiforms stepper using php and mysql
Thank you so much. I would like to know how to check multiple values, if telephone and email exists in the database. Thanks in advance
How to check if a time has been taken by other user? I don't know if I'm doing it correctly, but what I'm trying to do is to check if the time entered by the user is already taken by someone. Sample if on the DB as already start_time from 08:30:00AM and end_time is 10:00:00 the other user will not be able to add another time between this (time from 08:30:00AM to end_time is 10:00:00) will be have to choose another time like 07:00am to 08:29am or 10:01am to 12:00pm. I found some samples but I got no success.
This is what I got so far
Wow, nice job on all the documentation. Approaching age 70, I've been fooling with PHP since it was first available. Your PDO information is really helpful as I attempt to amuse or frustrate myself by migrating a basic personal project into PDO.
Reply:
Hello Doug!
I wish you best of luck with your enterprise! Do not hesitate to ask any questions, your questions let me make the site better
if (emailExists($email)) { should be fixed to if (emailExists($pdo, $email)) {
Reply:
Thank you a lot! Fixed.
I would usually write "SELECT COUNT() FROM users WHERE email=?" to check if a value exist. Is there any reason to use 1 instead of COUNT() ?
Thank you, so helpful. <3
Your site is incredible!!! I think it's the best explained on the web. I hope it stays online for along time. I'm new to PHP and you've helped me a lot. I would ask if you could do an example on PHPmailer. I need to get the registration form error array over to the PHPmailer code and don't send an email if the form has errors. You really are very good at PHP. Thanks for your great service.