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
How to Get Filename from a Variable (Url) in PHP
Display Data from Database to Dropdown Codeigniter
Convert Accented Characters to Their Plain Ascii Equivalents
How to Add Class to Link in Wp_Nav_Menu
Setting Selected Option in Laravel Form
Encoding CSV File With Arabic Content
How to Convert Jquery Ajax Success Data to a PHP Variable
How to Properly Encode Utf-8 for JavaScript and Json
Mysql Combine Two Columns into One in Query
How to Get Max and Min Value in a Multidimensional Array
Codeigniter Call to a Member Function Num Rows on Boolean
Comparing Two Dates in a SQL Request
Windows 10 Docker:/Usr/Bin/Env: 'Php\R': No Such File or Directory
Codeigniter - Call to a Member Function Result() on Boolean In
How to Get Comma Separated Values from a Table in Laravel
Auto Update Cart on Click in Woocommerce
Symfony: an Exception Occured in Driver: Could Not Find Driver With MySQL