0

I have a small problem. When I try to login on a script I worked on I can not login. I hope you guys can help me out. For some reason I get this MySQL error:

Call to undefined method mysqli_stmt::get_result() in home/[username]/public_html/inc/session.php on line 43

The code for 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);

and the code for 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');
}

I hope you guys can help me out. Thanks in advance,

Bram

neophyte
  • 6,143
  • 2
  • 22
  • 42
  • You know, indicating in your code which line is *43* would be super helpful... – random_user_name Jan 25 '17 at 00:52
  • The first result for `Call to undefined method mysqli_stmt::get_result()` in a Google Search is this: http://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result – random_user_name Jan 25 '17 at 00:53

1 Answers1

0

I think it's because of the version of PHP that you are using.

As mentioned in php documentation mysqli_stmt::get_result, this method is supported since PHP 5.3.0.

And it is stated in the user notes section that:

This method requires the mysqlnd driver. Othervise you will get this error: Call to undefined method mysqli_stmt::get_result()

Instead of this function, try using bind_result function.

Helpful link

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

neophyte
  • 6,143
  • 2
  • 22
  • 42