警告:mysqli_num_rows()期望参数1为mysqli_result,在行中给出null

时间:2018-03-27 08:37:00

标签: php mysql wampserver

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT user_id FROM user WHERE username = $myusername and password = $mypassword";
      $result = mysqli_query($db,$sql);
      $rows = mysqli_fetch_array($result);
      $active = $rows["user_id"];

      $count = mysqli_num_rows($active);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: index.php");
      }else {
         $error = "Your Account Credentials is Invalid, Please Try Again";
      }
   }
?>

我是PHP新手,有一个新问题。我是用mysqli开始新鲜的。 我想用php创建一个简单的登录表单。有人可以帮我解决这个错误吗?

2 个答案:

答案 0 :(得分:0)

mysqli_num_rows($result)包含一个参数$result,它指定mysqli_query()返回的结果集标识符。

这是您修改后的代码:

<?php
    include("config.php");
    session_start();

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 

        $myusername = mysqli_real_escape_string($db,$_POST['username']);
        $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

        $sql = "SELECT user_id FROM user WHERE username = $myusername and password = $mypassword";
        $result = mysqli_query($db,$sql);
        $rows = mysqli_fetch_array($result);
        $active = $rows["user_id"];

        $count = mysqli_num_rows($result);

        // If result matched $myusername and $mypassword, table row must be 1 row

        if($count == 1) {
            //session_register("myusername");
            $_SESSION['login_user'] = $myusername;

            header("location: index.php");
        }else {
            $error = "Your Account Credentials is Invalid, Please Try Again";
      }
   }
?>

有关详情,请访问mysqli_num_rows($result)

答案 1 :(得分:-1)

替换

$count = mysqli_num_rows($active);

通过

$count = mysqli_num_rows($result);