$ rownum = mysqli_num_rows($ result);返回0但必须返回1

时间:2016-03-29 08:00:17

标签: php login

我的表格中有一行,我为登录页面编写了此代码。不幸的是,它正在处理用户的登录页面,但不在管理员登录页面上。

if ($result = mysqli_query($connection, "SELECT * FROM admin WHERE username = '$username' AND password = '$password'")) {
        $rownum = mysqli_num_rows($result);
        if($rownum == 1){
             $_SESSION['print_admin'] = 'ok';
             header("Location: ../admin/index.php");
             die();
        } else {
             echo "password is wrong";
        }
    }

$rownum返回0,但用户名和密码在表格中以行的形式存在

2 个答案:

答案 0 :(得分:-1)

试试这个:

   if ($result = mysqli_query($connection, "SELECT * FROM admin WHERE username = '$username' AND password = '$password'")) {


     if(mysqli_num_rows($result)){
     $_SESSION['print_admin'] = 'ok';
     header("Location: ../admin/index.php");
     die();
   }
    else {
            echo "password is wrong";
   }
}

答案 1 :(得分:-1)

试试这个:

$sql = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."'";
echo $sql; //copy and test using phpmyadmin :D

if ($result = mysqli_query($connection, $sql)) {

  $rownum = mysqli_num_rows($result);
  if($rownum == 1){
      $_SESSION['print_admin'] = 'ok';
      header("Location: ../admin/index.php");
      die();
  }
  else {
      echo "password is wrong";
  }
}