Check to see if an email is already in the database using prepared statements

三世轮回 提交于 2019-11-29 15:06:09
Rachael

Should be something like this:

// create mysqli object
$mysqli = new mysqli(/* fill in your connection info here */);

$email = $_POST['email']; // might want to validate and sanitize this first before passing to database...

// set query
$query = "SELECT COUNT(*) FROM users WHERE email = ?"

// prepare the query, bind the variable and execute
$stmt = $mysqli->prepare( $query );
$stmt->bind_param( 's', $email );
$stmt->execute()

// grab the result
$stmt->store_result();

// get the count
$numRows = $stmt->num_rows();

if( $numRows )
{
     echo "<p class='red'>Email is already registered with us</p>";
}
else
{
    // ....
}

This link may help you as well:

http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!