mysqli_query() 总是返回真

2021-12-25 00:00:00 php mysql mysqli

这是我的表格:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <form action="register_ajax.php" method="get">
    <input type="text" name="email">
    <input type="submit" value="test">
    </form>
  </body>
</html>

这是我的php代码:

<?php
    $dbc = mysqli_connect("localhost","root","*******","continental_tourism") OR die(mysqli_connect_error());
    $email = $_GET['email'];
    $query = "SELECT email FROM customer_info WHERE email = '$email' ";
    $r = mysqli_query($dbc, $query) OR die(mysqli_error($dbc));
    if($r)
        echo "Email address exists!";
    else
        echo "sss";
?>

如果我输入正确的(db 上的现有电子邮件)$r 为真.但是如果我输入不存在的电子邮件,那么 $r 也是如此.这是为什么?基本上我想检测空集.我该怎么做?

If I enter a correct(Existing email on db) $r is true. But if I enter non existing email, then also $r is true. Why is that? Basically I want to detect the empty set. How can I do it?

谢谢!

推荐答案

$r 只有在出现 SQL 错误时才会为假.否则它将始终返回一个对象,即使您的 SELECT 语句没有返回任何行.

$r will only be false if there was SQL error. Otherwise it will always return an object, even if no rows are returned by your SELECT statement.

使用mysqli_num_rows() 计算返回的行数.零表示没有人使用该电子邮件地址.

Use mysqli_num_rows() to count how many rows are returned. Zero means no one is using that email address.

if(mysqli_num_rows($r))
    echo "Email address exists!";
else
    echo "sss"; 

相关文章