检查数据库mysqli_num_rows中是否存在

时间:2017-09-20 11:04:15

标签: php mysql

$check = mysqli_real_escape_string($link, $_REQUEST['email']);
$sql = "SELECT * FROM users WHERE email = '$check'";
if (mysqli_num_rows($sql) == 0)
{
 echo "1";
}
else 
{
echo "0";
}

我正在尝试检查数据库中是否存在电子邮件,但无论我为if (mysqli_num_rows($sql) == 0添加了什么价值,如果电子邮件存在与否,我只会查看结果10。 一些想法?

3 个答案:

答案 0 :(得分:0)

您没有使用mysqli_query

运行sql语句

您必须首先查询sql语句然后计算行

$conn = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
/*mysqli connection*/

$check = mysqli_real_escape_string($link, $_REQUEST['email']);
    $sql = "SELECT * FROM users WHERE email = '$check'";
    $res = mysqli_query($conn,$sql);
      if (mysqli_num_rows($res ) == 0){
    }

答案 1 :(得分:0)

执行您的查询:

$sql = "SELECT * FROM users WHERE email = '$check'";
$result=mysqli_query($con,$sql)
if (mysqli_num_rows($result) == 0)

答案 2 :(得分:0)

if (mysqli_num_rows($results) == 1) {
  $_SESSION['username'] = $user_id;
  $_SESSION['success'] = "You are now logged in";
  header('location: index.php');
}else {
  array_push($errors, "Wrong credentials");
}