为什么mysqli_num_rows总是返回false [duplicate]

why does mysqli_num_rows function always returns false in code below?

<?php
if($_POST['submit']){
  //to validate the email address

  if(!$_POST['email']){
    $error.="<br/>Please enter your email";
  }else if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
    $error.="<br/>Please enter a valid email address";
  }

  // To validate password
  if(!$_POST['password']){
    $error.="<br/>Please enter a password";
  }else{
    if(strlen($_POST['password'])<8){
      $error.="<br/>Please enter a password with at least 8 characters";
    }
    if(!preg_match('`[A-Z]`',$_POST['password'])){
      $error.="<br/>Please include at least one capital letter in your password";
    }
  }

  if($error){
    echo "There were error(s) in your sign up details".$error;
  }else{

    $link=mysqli_connect("localhost","diary","");
    if(mysqli_connect_error()){
      die("there is an arror");
    }

    //$query="insert into `users` (`email`,`number`) values ('asd@asad.com',123)";

    $email1=$_POST['email'];

    // search for already registered emails
    $query="select * from users where email='".mysqli_real_escape_string($link,$email1)."'";

    $result = mysqli_query($link, $query);

    if (!$result){
      die(mysqli_error($link));
    }

    // And here where errors comes.
    $results = mysqli_num_rows($result);

    if(!$results){
      echo "That email address is already registered";
    }else{
      echo "dont know";
    }
  }
}
?>

<form method=post>
  <input type="email" name="email" id="email"/>
  <input type="password" name="password" />
  <input type="submit" name="submit" value="Sign Up">
</form>
</div>

If this is not your whole script, you need to initialize error to some value before concatenating stuff to it.

$error = "";

Second, you don't bother checking the result of your query before you feed it to mysqli_num_rows. You should always check the value of a query before trying to do anything else with it.

$query="select * from users where email='".mysqli_real_escape_string($link,$email1)."'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
if (!$result) {
    // you should write this to an error log. don't show it to a user
    $error = mysqli_error($link);
    die("The query failed!");
}

// And here where errors comes.
$results = mysqli_num_rows($result);