Last active
January 23, 2019 20:21
-
-
Save devluis/7539161 to your computer and use it in GitHub Desktop.
Script to check if email exist using PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
DEFINE('DB_USER', 'YOUR_DATA_BASE_USER'); | |
DEFINE('DB_PASSWORD', 'YOUR_DATA_BASE_PASSWORD'); | |
DEFINE('DB_HOST','YOUR_HOST'); | |
DEFINE('DB_NAME','YOUR_DATA_BASE_NAME'); | |
$email = 'email-to-search@mydomain.com'; | |
$con = new PDO( 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=UTF-8',DB_USER,DB_PASSWORD ); | |
$query = $con->prepare( "SELECT email | |
FROM your_users_table | |
WHERE email = ?" ); | |
$query->bindValue( 1, $email ); | |
$query->execute(); | |
if( $query->rowCount() > 0 ) { # If rows are found for query | |
echo "Email found!"; | |
} | |
else { | |
echo "Email not found!"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to manual: For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.