php - Email is already in database, returns as if it isn't -
here background information on i'm trying here. i'm try create registration form website (successful far until point). data can entered db. thing i'm trying prevent duplicate email/usernames being entered db. through stackoverflow research, have found , tested following code:
$query = "select count(*) num_rows users email = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param("s", $email);  if ($stmt->execute()) {     return $stmt->num_rows; }   what following:
if(user_exists($user_email) > 0) {     echo "email exists!"; }   but passes if statement if email exist in database! email i'm trying enter, tests testemail@testemailweb.com in database! possibly point out have messed in code? there silly mistake have possibly done when trying perform this? 
the fix particular problem here not using count(*) mentioned john , not depend on mysqli_stmt->num_rows using buffered result set:
$query = "select * users email = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param("s", $email);  return $stmt->execute() && $stmt->store_result() && $stmt->num_rows > 0;   addendum
the thing i'm trying prevent duplicate email/usernames being entered db
you want use table constraints prevent (not app, else can access database). has added benefit of guarding against race conditions.
alter table users add unique(email);   this raise error if attempt insert row email value exists. can check error on application side , whatever want it.
Comments
Post a Comment