mysqli错误 - 获取数组期望参数为1,给定字符串

时间:2014-04-05 15:45:51

标签: php mysqli

为什么这不起作用? 我使用了一个教程来做到这一点。

// FETCH DATA FROM INPUT FIELD
$user = mysqli_real_escape_string($db2, $_POST['user']);
$pass = mysqli_real_escape_string($db2, $_POST['pass']);

  // CHECK ALL FIELD HAS BEEN FILLED UP
 if ($user && $pass) {

       // QUERY FROM DATABASE
  $query= mysqli_query($db2, "SELECT * FROM members WHERE username='".$user."'");
  $checkuser= mysqli_num_rows($query);

   // CHECK IF USERNAME EXIST ON DATABASE
  if($checkuser != 1) {

    // I'LL BE SETTING A VARIABLE IF YOUR DOESN'T EXIST
   $error = "Username doesn't exist in our database!";
  }

   // FETCHING PASSWORD IN DATABASE WHERE USERNAME COINCIDES
 while  ($row = mysqli_fetch_array($user)) {
   $checkpass= $row['password'];


    // CHECK IF ENTERED PASSWORD MEETS THE USERNAME PASSWORD
   if ($pass== $checkpass) {

     // IF ALL OKAY SET SESSION
    setcookie("user", $user, time()+7200);
    $_SESSION['user'] = $user;
    $_SESSION['start'] = time();
    $_SESSION['expire'] = $_SESSION['start'] + (60 * 60 * 60);
    header("Location: ".$_SERVER['PHP_SELF']);
    exit();
   } else {

     // SET VARIABLE THAT'LL SHOW IF USER PASSWORD IS INCORRECT
    $error = "Incorrect password!";
   }
  }
 } else {

  // SET VARIABLE IF ALL FIELD ARE NOT FILLED UP
 $error = "Please enter a username and password.";
 }
}

?>

2 个答案:

答案 0 :(得分:0)

替换

 while  ($row = mysqli_fetch_array($user))

while  ($row = mysqli_fetch_array($query))

你得到标题错误,因为这段代码

 setcookie("user", $user, time()+7200);
    $_SESSION['user'] = $user;
    $_SESSION['start'] = time();
    $_SESSION['expire'] = $_SESSION['start'] + (60 * 60 * 60);
    header("Location: ".$_SERVER['PHP_SELF']);

应该在任何输出之前

答案 1 :(得分:0)

作为一般经验法则,当你想使用标题(...)时;你必须确保php还没有开始输出任何东西。

这意味着:没有echo,print_r,var_dump ......但是请记住,php文件输出的不是之间的一切。所以最后。我说的是在实际标题(...)之前可能包含的所有文件;

看来你在login.php:11

的第11行输出了一些东西