Skip to content

Instantly share code, notes, and snippets.

@devluis
Last active January 23, 2019 20:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devluis/7539161 to your computer and use it in GitHub Desktop.
Save devluis/7539161 to your computer and use it in GitHub Desktop.
Script to check if email exist using PDO
<?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!";
}
?>
@rumblefrog
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment