mysqli_query() always return true

前端 未结 4 1122
清歌不尽
清歌不尽 2021-01-03 10:58

This is my form:



  
  
  
    
相关标签:
4条回答
  • 2021-01-03 11:19

    mysqli_query() will always returns a MySQLi result resource if the query was successful. A resource will always evaluate to TRUE when tested as a boolean.

    A successful query is not necessarily one that returns results, it is simply a query that did not fail. Queries are considered to have failed when there is an error, for example a syntax error or a processing error within the MySQL server - which case they will return FALSE. But a query that finds no results is still a successful query.

    You need to use mysqli_num_rows() to determine whether the query found any results:

    <?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"; 
    ?>
    
    0 讨论(0)
  • 2021-01-03 11:33

    mysqli_query will return a result object on success and false on failure. No rows selected is not considered failure. You can use mysqli_num_rows to check the number of rows returned.

    0 讨论(0)
  • 2021-01-03 11:38

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

    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"; 
    
    0 讨论(0)
  • 2021-01-03 11:41

    One needs to test to see if $r is true, and if row count is greater than 0.

    The following works:

    if ($r and mysqli_num_rows($r) > 0) {
    
      echo "rows exist"; } else {
    
    echo "rows don't exist";  }
    
    0 讨论(0)
提交回复
热议问题