php警告:mysqli_fetch_array()期望参数1为mysqli_result,给定布尔值

时间:2016-02-15 06:02:13

标签: php mysqli parameters boolean

我收到此错误

  

警告:mysqli_fetch_array()期望参数1为mysqli_result,
  第13,16行和第34行中给出的布尔值

在php中运行我的logincheck页面时,代码如下。

有人可以帮忙修复它吗?

include("conn.php");
session_start();

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

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

    $sql = "SELECT userid FROM user WHERE username = '$myusername' and passcode = '$mypassword'";
    $result = mysqli_query($db,$sql);
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    $active = $row['active'];

    $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: home.php");
    }else {
        $error = "Your Login Name or Password is invalid";
    }
}

3 个答案:

答案 0 :(得分:0)

您的查询失败并返回错误值。

在你的mysqli_query()之后看看这是怎么回事。

if (!$result) {
    printf("Error: %s\n", mysqli_error($db));
    exit();
}

如果您想了解更多信息,请参阅this

答案 1 :(得分:0)

您需要通过修改代码来检查是否有任何行被提取:

if(mysqli_num_rows($result) > 0){
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
}

如果没有找到row / s,它将不会将布尔值传递给mysqli_fetch_array()方法,因为mysqli_query()如果没有找到结果则返回布尔值...

答案 2 :(得分:0)

查询应为

$sql = "SELECT userid FROM user WHERE username = '".$myusername ."' and passcode = '".$mypassword."'";

而不是

$sql = "SELECT userid FROM user WHERE username = '$myusername' and passcode = '$mypassword'";