Mysqli_query () always returns true

This is my form:

<!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>

This is my php code:

<?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";
?>

If I enter the correct one (existing email address on db) $r, this is true. But if I enter a non-existing email, it $rwill also be true. Why is this? Basically I want to detect an empty set. How can i do this?

Thank!

+3
source share
3 answers

$rwill only be false if an SQL error occurs. Otherwise, it will always return the resource identifier, even if no rows are returned by the SELECT statement.

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

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

mysqli_query() MySQLi, . TRUE .

, , , . , , MySQL - FALSE. , , - .

mysqli_num_rows(), , - :

<?php
    $dbc = mysqli_connect("localhost","root","longhorn","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(mysqli_num_rows($r) > 0)
        echo "Email address exists!";
    else
        echo "sss"; 
?>
+4
0
source

All Articles