在第43行调用未定义的方法mysqli_stmt :: get_result()

时间:2017-01-25 00:40:14

标签: javascript php mysql mysqli

我有一个小问题。当我尝试登录我工作的脚本时,我无法登录。我希望你们能帮助我。出于某种原因,我得到了这个MySQL错误:

  

调用未定义的方法mysqli_stmt :: get_result()in   第43行的主页/ [用户名] /public_html/inc/session.php

session.php的代码:

<?php

class Session {
    private $self_file = 'session.php';
    private $mysqli = false;

    public function __construct($m) { $this->mysqli = $m; }

    public function isLogged() {
        if(!isset($_SESSION['invento_logged']) || !is_array($_SESSION['invento_logged']))
            return false;

        if(!isset($_SESSION['invento_logged']['u']) || !isset($_SESSION['invento_logged']['p']))
            return false;

        $u = $_SESSION['invento_logged']['u'];
        $p = $_SESSION['invento_logged']['p'];

        $prepared = $this->prepare("SELECT count(*) as c FROM invento_users WHERE username=? && password=?", 'isLogged()');
        $this->bind_param($prepared->bind_param('ss', $u, $p), 'isLogged()');
        $this->execute($prepared, 'isLogged()');

        $result = $prepared->get_result();
        $row = $result->fetch_object();

        if($row->c == 1)
            return true;
        return false;
    }

    public function refresh_password($pass) {
        $_SESSION['invento_logged']['p'] = md5($pass);
        return true;
    }

    public function login($u, $p) {
        $p = md5($p);

        $prepared = $this->prepare("SELECT count(*) as c FROM invento_users WHERE username=? && password=?", 'isLogged()');
        $this->bind_param($prepared->bind_param('ss', $u, $p), 'login()');
        $this->execute($prepared, 'login()');

        $result = $prepared->get_result();
        $row = $result->fetch_object();

        if($row->c != 1)
            return false;

        $_SESSION['invento_logged']['u'] = $u;
        $_SESSION['invento_logged']['p'] = $p;

        return true;
    }

    public function logout() {
        if(isset($_SESSION['invento_logged']))
            $_SESSION['invento_logged'] = false;
        unset($_SESSION);
        session_destroy();
        return true;
    }

    public function get_user_id() {
        $username = $_SESSION['invento_logged']['u'];

        $prepared = $this->prepare("SELECT id FROM invento_users WHERE username=?", 'get_user_id()');
        $this->bind_param($prepared->bind_param('s', $username), 'get_user_id()');
        $this->execute($prepared, 'get_user_id()');

        $result = $prepared->get_result();
        $row = $result->fetch_object();

        return $row->id;
    }

    public function get_user_name_by_id($id) {
        $prepared = $this->prepare("SELECT username FROM invento_users WHERE id=?", 'get_user_name_by_id()');
        $this->bind_param($prepared->bind_param('i', $id), 'get_user_name_by_id()');
        $this->execute($prepared, 'get_user_name_by_id()');

        $result = $prepared->get_result();
        $row = $result->fetch_object();

        return $row->username;
    }

    public function get_user_role() {
        $id = $this->get_user_id();

        $prepared = $this->prepare("SELECT role FROM invento_users WHERE id=?", 'get_user_role()');
        $this->bind_param($prepared->bind_param('i', $id), 'get_user_role()');
        $this->execute($prepared, 'get_user_role()');

        $result = $prepared->get_result();
        $row = $result->fetch_object();

        return $row->role;
    }


    /***
      *  Private functions
      *
    ***/
    private function prepare($query, $func) {
        $prepared = $this->mysqli->prepare($query);
        if(!$prepared)
            die("Couldn't prepare query. inc/{$this->self_file} - $func");
        return $prepared;
    }
    private function bind_param($param, $func) {
        if(!$param)
            die("Couldn't bind parameters. inc/{$this->self_file} - $func");
        return $param;
    }
    private function execute($prepared, $func) {
        $exec = $prepared->execute();
        if(!$exec)
            die("Couldn't execute query. inc/{$this->self_file} - $func");
        return $exec;
    }
    private function query($query, $func) {
        $q = $this->mysqli->query($query);
        if(!$q)
            die("Couldn't run query. inc/{$this->self_file} - $func");
        return $q;
    }
    public function __destruct() {
        if(is_resource($this->mysqli) && get_resource_type($this->mysqli) == 'mysql link')
            $this->mysqli->close();
    }
}

$_session = new Session($mysqli);

和config.php的代码:

<?php
session_start();

/************   You can edit details starting from here ************/
$dbhost = '(I've filled this in';       // Write your MySQL host here.
$dbuser = 'I've filled this in';    // Write your MySQL User here.
$dbpass = 'I've filled this in';    // Write your MySQL Password here.
$dbname = 'I've filled this in';        // Write the MySQL Database where you want to install


/************ DON'T EDIT NOTHING BELOW ************/




if(!isset($noredir) && $dbhost == 'localhost' && $dbuser == 'MYSQL USERNAME' && $dbpass == 'MYSQL PASSWORD')
    header('Location:install.php');
if(!isset($noredir)) {
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if($mysqli->connect_errno)
        die('<h2>Something went wrong while trying to connect to your MySQL Database. Error No. ' . $mysql->connect_errno.'<h2>');

    // Check existance of random table to test installed system
    $tables = array('users','categories','items','logs','settings');
    $rn = rand(0,4);
    $res = $mysqli->query("SHOW TABLES LIKE '%invento_{$tables[$rn]}%'");
    if($res->num_rows == 0)
        header('Location:install.php');
}

我希望你们能帮助我。 提前谢谢,

的Bram

1 个答案:

答案 0 :(得分:0)

我认为这是因为您使用的是PHP版本。

如php文档mysqli_stmt::get_result中所述,自PHP 5.3.0起支持此方法。

用户备注部分说明:

此方法需要mysqlnd驱动程序。 Othervise你会得到这个错误:调用未定义的方法mysqli_stmt :: get_result()

尝试使用bind_result函数。

而不是此功能

有用的链接

http://php.net/manual/en/mysqli-stmt.get-result.php