Checking If Email Is in MySQL Database in PHP

php check if email exists in mysql database

You just need to add $stmt->store_result();

function emailExists($conn, $email) {
$stmt = $conn->prepare("SELECT 1 FROM accountInfo WHERE email=? LIMIT 1");
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows;
}

Check this PHP code here

Checking if user & email exists on MYSQL with PHP

I edited first part of your registNew function to check if there are any results. Everything is of course escaped so you are safe.

function registNew() {
$con = mysqli_connect("localhost","root","","work");
$username = mysqli_real_escape_string($con, $_POST["username"]);
$password = mysqli_real_escape_string($con, $_POST["password"]);
$email = mysqli_real_escape_string($con, $_POST["email"]);

if(mysqli_connect_errno())
{
echo "Error MySQL: " .mysqli_connect_errno();
}

$rsUsers = mysqli_query($con,"SELECT * FROM users WHERE username = '".$username."'");
$rsEmails = mysqli_query($con,"SELECT * FROM users WHERE email = '".$email."'");
$numUsers = mysqli_num_rows($rsUsers);
$numEmails = mysqli_num_rows($rsEmails);

if($numUsers > 0 || $numEmails > 0) {
echo "User already exists";
}
else
{

$newUser= "INSERT INTO users(username,password,email) VALUES('$username','$password','$email')";
if(mysqli_query($con,$newUser))
{
echo "Account has been created<br/>";
/* header('Location: login.php'); */
}
else
{
echo "Error at adding user<br/>";
header("refresh:5;url=register.php");
}

}
}

check if email exists in MySQL database

if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
exit('Invalid email address'); // Use your own error handling ;)
}
$select = mysqli_query($connectionID, "SELECT `email` FROM `game` WHERE `email` = '".$_POST['email']."'") or exit(mysqli_error($connectionID));
if(mysqli_num_rows($select)) {
exit('This email is already being used');
}

Stick that in above the insert query

PHP how to check for email already in MySQL database?

if email is an unique key, that would be simple

<?php
mysql_connect("localhost","root","root");
mysql_select_db("howdini");
$email = mysql_real_escape_string($_POST['mail']);
$sql="INSERT IGNORE INTO newsletter (email) VALUES ('$email')";
mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if (mysql_affected_rows()) {
header("Location: /thankyoupage.php"); //Redirect page
} else {
//already exists
}

PHP mySQL if statement - check if email already in db

You just have a typo in your PHP script, change

if(mysqli_num_rows($query>0))

Into

if(mysqli_num_rows($query) > 0)

check that if email exists in data base or not

You can query this to know if a user is already registered.

SELECT gmail
FROM users
WHERE gmail = '$gmail'

If you have 1 row returned, so this user is already registered.

Once you find if this user exists, you can do your query with the INSERT clause.

Using PHP Check Email ID Already Exist Or Not in Database

A good way is to create a UNIQUE index on the email column, then you could use INSERT IGNORE INTO instead of simple insert, and then you could check affected_rows to determine if the insertion was successful or the email already exists in the table:

$stmt = mysqli_prepare($conn, "INSERT IGNORE INTO people (email) VALUES (?);");

mysqli_stmt_bind_param($stmt, 's', $email);
$result = mysqli_stmt_execute($stmt);

if(mysqli_affected_rows($result) === 1) {
// email was new, here you should send the email
$mail = new PHPMailer;
}
else {
echo "This email has already been registered";
}


Related Topics



Leave a reply



Submit