在boolean上调用成员函数bind_param()(MySQLi未运行)

时间:2016-10-21 09:15:27

标签: php mysql

我正在尝试创建一个登录脚本,用于收集用户发布的信息,然后在用户提供相关信息时创建会话。每次我尝试登录时都会收到错误:

PHP Fatal error: Call to a member function bind_param() on boolean

我在下面提供了我的代码:     要求' ../../../ db-config.php&#39 ;;     要求' ../ distributor-config.php';

$user_role = $_POST['user_role'];
$user_username = $_POST['user_username'];
$user_password = $_POST['user_password'];

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ?, username = ?, password = ?");
$stmt->bind_param("sss", $user_role, $user_username, $user_password);
$stmt->execute();

$stmt->bind_result($role, $id, $emailaddress, $username, $firstname, $surname);

while($stmt->fetch()) {
    if($stmt == 1 && $user_role == 'Manager') {
        session_start();
        $_SESSION['userinfo']['role'] = $id;
        $_SESSION['userinfo']['id'] = $id;
        $_SESSION['userinfo']['emailaddress'] = $emailaddress;
        $_SESSION['userinfo']['username'] = $username;
        $_SESSION['userinfo']['firstname'] = $firstname;
        $_SESSION['userinfo']['surname'] = $surname;

        header('location: ../manager-index.php?page=login&status=success');
    }

    elseif($stmt == 1 && $user_role == 'User') {
        session_start();
        $_SESSION['userinfo']['role'] = $id;
        $_SESSION['userinfo']['id'] = $id;
        $_SESSION['userinfo']['emailaddress'] = $emailaddress;
        $_SESSION['userinfo']['username'] = $username;
        $_SESSION['userinfo']['firstname'] = $firstname;
        $_SESSION['userinfo']['surname'] = $surname;

        header('location: ../user-index.php?page=login&status=success');
    }

    else {
        header('location: ../login.php?page=login&status=error');
    }
}

$stmt->close();
$conn->close();

3 个答案:

答案 0 :(得分:0)

问题出在SQL语句中。如果SQL失败,则返回FALSE

请参阅:stackoverflow.com

public function render(View $view, $route = null, $obj = null, $method = 'POST') {
$driverTypeList = DriverType::getArray();
$view->with(compact('driverTypeList'));
return parent::render($view, $route, $obj, $method);
}

答案 1 :(得分:0)

这是错误的:

  

$ stmt = $ conn-> prepare(“SELECT role,id,emailaddress,username,   firstname,surname FROM users WHERE role = ?, username =?,password =   ?“);

应该是:

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ? AND username = ? AND  password = ?");

答案 2 :(得分:0)

查看此地点删除逗号,添加并在条件

$stmt = $conn->prepare("SELECT role, id, emailaddress, username, firstname, surname FROM users WHERE role = ? AND username = ? AND  password = ?");

Docs refer the mysqli prepared statement

注意:

1)session_start();应该是一次,也应该在页面最顶层