php - check if row exists with mysql -
i need checking if row exists. getting "email no longer exists publisher@example.com".
is there better way check if row exists mysqli?
if (count($_post)) { $email = $dbl->real_escape_string(trim(strip_tags($_post['email']))); $passwd = $dbl->real_escape_string(trim(strip_tags($_post['passwd']))); $query = "select `email` `tbluser` `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"; } }
the following tried, tested , proven methods check if row exists.
(some of use myself, or have used in past).
edit: made previous error in syntax used mysqli_query()
twice. please consult revision(s).
i.e.:
if (!mysqli_query($con,$query))
should have read if (!$query)
.
- i apologize overlooking mistake.
side note: both '".$var."'
, '$var'
same thing. can use either one, both valid syntax.
here 2 edited queries:
$query = mysqli_query($con, "select * emails email='".$email."'"); if (!$query) { die('error: ' . mysqli_error($con)); } if(mysqli_num_rows($query) > 0){ echo "email exists"; }else{ // }
and in case:
$query = mysqli_query($dbl, "select * `tbluser` email='".$email."'"); if (!$query) { die('error: ' . mysqli_error($dbl)); } if(mysqli_num_rows($query) > 0){ echo "email exists"; }else{ // }
you can use mysqli_
prepared statement method:
$query = "select `email` `tbluser` 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 exists."; exit; } } }
or pdo method 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 named submit button if(isset($_post['submit'])) { try { $stmt = $conn->prepare('select `email` `tbluser` 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 non-existant."; } } ?>
- prepared statements best used protect against sql injection.
n.b.:
when dealing forms , post arrays used/outlined above, make sure post arrays contain values, post method used form , matching named attributes inputs.
- fyi: forms default method if not explicity instructed.
note: <input type = "text" name = "var">
- $_post['var']
match. $_post['var']
no match.
- post arrays case-sensitive.
consult:
error checking references:
- http://php.net/manual/en/function.error-reporting.php
- http://php.net/manual/en/mysqli.error.php
- http://php.net/manual/en/pdo.error-handling.php
please note mysql apis not intermix, in case may visiting q&a , you're using mysql_
connect (and querying with).
- you must use same 1 connecting querying.
consult following this:
if using mysql_
api , have no choice work it, consult following q&a on stack:
the mysql_*
functions deprecated , removed future php releases.
- it's time step 21st century.
you can add unique constraint (a) row(s).
references:
Comments
Post a Comment