检查数据库中是否存在电子邮件

时间:2016-03-30 04:30:38

标签: php mysqli

我正在尝试在表单

上键入电子邮件后检查数据库中是否存在电子邮件地址

这是我的代码

$emailbox = $_POST['loginemailbox'];
$check_email =mysqli_query(" SELECT * FROM Registration WHERE Email = '$emailbox'"); 

if(mysqli_num_rows($check_email) == 1)
  {
      echo $emailbox." is found in the database :)";
  } else 
  {
    echo $emailbox."is not found in database :(";
   }

我在这里做错了什么,任何人都可以向我指出错误

3 个答案:

答案 0 :(得分:1)

  

注意:PDO最适合查询。正如您在mysqli使用的那样   必须使用准备声明以获得最佳性能和良好/智能   看。我就像你正在做的那样回答你的问题。

建立数据库连接,在查询中使用它。

尝试这样的事情:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Perform queries
$check_email = mysqli_query($con, "SELECT * FROM Registration WHERE Email = '$emailbox'");
if(mysqli_num_rows($check_email)){
    echo $emailbox." is found in the database :)";
}else{
  echo $emailbox."is not found in database :(";
}

mysqli_close($con);

<强>输出

输出来自sqlfiddle

答案 1 :(得分:0)

我认为你遗失了mysqli_query($connection, $query) 试试:

$connection = mysqli_connect('hostname','db_user','db_pass','db_name');
$check_email = mysqli_query($connection, "SELECT * FROM Registration WHERE Email = '$emailbox'"); 

更多详情:Mysqli_Query

答案 2 :(得分:0)

如果你已经检查过

1.在代码中添加连接

2.检查表名和字段名

请检查

1.保存在数据库中的电子邮件之前和之后的空格。

2.trim您发布的电子邮件... trim($ emailbox)