I need help checking if a row exists in the database. In my case, that row contains an email address. I am getting the result:
email no longer exists [email protected]
This is the code I'm currently using:
if (count($_POST)) { $email = $dbl->real_escape_string(trim(strip_tags($_POST['email']))); $query = "SELECT `email` FROM `tblUser` WHERE `email` = '$email'"; $result = mysqli_query($dbl, $query); if (is_resource($result) && mysqli_num_rows($result) == 1) { $row = mysqli_fetch_assoc($result); echo $email . " email exists " . $row["email"] . "\n"; } else { echo "email no longer exists" . $email . "\n"; } }
Is there a better way to check if a row exists in MySQL database (in my case, check if an email exists in MySQL)?
To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.
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. Here we are selecting a row matching our criteria, then fetching it and then checking whether anything has been selected or not.
The following are tried, tested and proven methods to check if a row exists.
(Some of which I use myself, or have used in the past).
Edit: I made an previous error in my syntax where I used mysqli_query()
twice. Please consult the revision(s).
I.e.:
if (!mysqli_query($con,$query))
which should have simply read as if (!$query)
.
Side note: Both '".$var."'
and '$var'
do the same thing. You can use either one, both are valid syntax.
Here are the two edited queries:
$query = mysqli_query($con, "SELECT * FROM emails WHERE email='".$email."'"); if (!$query) { die('Error: ' . mysqli_error($con)); } if(mysqli_num_rows($query) > 0){ echo "email already exists"; }else{ // do something }
and in your case:
$query = mysqli_query($dbl, "SELECT * FROM `tblUser` WHERE email='".$email."'"); if (!$query) { die('Error: ' . mysqli_error($dbl)); } if(mysqli_num_rows($query) > 0){ echo "email already exists"; }else{ // do something }
You can also use mysqli_
with a prepared statement method:
$query = "SELECT `email` FROM `tblUser` WHERE email=?"; if ($stmt = $dbl->prepare($query)){ $stmt->bind_param("s", $email); if($stmt->execute()){ $stmt->store_result(); $email_check= ""; $stmt->bind_result($email_check); $stmt->fetch(); if ($stmt->num_rows == 1){ echo "That Email already exists."; exit; } } }
Or a PDO method with a prepared statement:
<?php $email = $_POST['email']; $mysql_hostname = 'xxx'; $mysql_username = 'xxx'; $mysql_password = 'xxx'; $mysql_dbname = 'xxx'; try { $conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { exit( $e->getMessage() ); } // assuming a named submit button if(isset($_POST['submit'])) { try { $stmt = $conn->prepare('SELECT `email` FROM `tblUser` WHERE email = ?'); $stmt->bindParam(1, $_POST['email']); $stmt->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { } } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } if($stmt->rowCount() > 0){ echo "The record exists!"; } else { echo "The record is non-existant."; } } ?>
N.B.:
When dealing with forms and POST arrays as used/outlined above, make sure that the POST arrays contain values, that a POST method is used for the form and matching named attributes for the inputs.
Note: <input type = "text" name = "var">
- $_POST['var']
match. $_POST['Var']
no match.
Consult:
Error checking references:
Please note that MySQL APIs do not intermix, in case you may be visiting this Q&A and you're using mysql_
to connect with (and querying with).
Consult the following about this:
If you are using the mysql_
API and have no choice to work with it, then consult the following Q&A on Stack:
The mysql_*
functions are deprecated and will be removed from future PHP releases.
You can also add a UNIQUE constraint to (a) row(s).
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With