dongteng0748 2017-01-06 17:16
浏览 59
已采纳

为什么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>
  • 写回答

1条回答 默认 最新

  • dpgkg42484 2017-01-06 17:41
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流