加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用预准备语句检查数据库中是否已有电子邮件

发布时间:2020-12-13 13:41:48 所属栏目:PHP教程 来源:网络整理
导读:我试图将我的代码从 mysql更改为msqli预处理语句.我不知道如何调整我目前工作的代码以检查数据库中是否已有电子邮件.以下是我目前正在使用的代码.如何将其更改为准备好的语句并获得相同的结果? //if email is equal to an email already in the database,di
我试图将我的代码从 mysql更改为msqli预处理语句.我不知道如何调整我目前工作的代码以检查数据库中是否已有电子邮件.以下是我目前正在使用的代码.如何将其更改为准备好的语句并获得相同的结果?
//if email is equal to an email already in the database,display an error message

if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE email = '".mysql_real_escape_string($_POST['email'])."'")))
{
  echo "<p class='red'>Email is already registered with us</p>";
} else {
  // missing code?
}
应该是这样的:
// 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
{
    // ....
}

此链接也可以帮助您:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读