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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀