Pass mysql class to mysqli error in query

0

I'm having a problem passing the query (this->Query($sql)) and the others to mysqli . I do not know how to proceed

ERRORS:

  

PHP Warning: mysqli_query () expects parameter 1 to be mysqli, null   given in /var/www/html/manager/include/class.mysqldb.php on line 23   PHP Warning: mysqli_fetch_object () expects parameter 1 to be   mysqli_result, null given in   /var/www/html/manager/include/class.mysqldb.php on line 27 PHP   Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result,   null given in /var/www/html/manager/include/class.mysqldb.php on line   31

<?php
class mysqldb {
    var $link;
    var $result;
    function connect($config) {
        $this->link = mysqli_connect($config['hostname'], $config['username'], $config['password'], $config['database']);
        if ($this->link) {
            mysqli_query($this->link, "SET NAMES 'utf-8'");
            return true;
        }
        $this->show_error(mysqli_error($this->link), "connect()");
        return false;
    }
    function selectdb($database) {
        if ($this->link) {
            mysqli_select_db($this->link, $database);
            return true;
        }
        $this->show_error("Not connect the database before", "selectdb($database)");
        return false;
    }
    function query($sql) {
        $this->query = mysqli_query($this->link, $sql);
        return $this->query;
    }
    function fetch() {
        $result = mysqli_fetch_object($this->query);
        return $result;
    }
    function num_rows() {
        return mysqli_num_rows($this->query);
    }
    function show_error($errmsg, $func) {
        echo "<b><font color=red>" . $func . "</font></b> : " . $errmsg . "<BR>\n";
        exit(1);
    }
}
?>

How do I call the class

<?php
# configuration for database
$_config['database']['hostname'] = "localhost";
$_config['database']['username'] = "root";
$_config['database']['password'] = "senha";
$_config['database']['database'] = "banco";

# connect the database server
$link = new mysqldb();
$link->connect($_config['database']);
$link->selectdb($_config['database']['database']);
$link->query("SET NAMES 'utf8'");

@session_start();

? >

    
asked by anonymous 15.10.2018 / 19:52

1 answer

0

I think the problem is in your configuration file that connects to the database that is still in the old template.

$link = new mysqldb();

Try this way below with mysqli, I always use it and never had a problem with PHP versions:

$link = new mysqli('localhost', $_USER, $_PASS, $_DB);
 $link->set_charset("utf8");

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error()); }

mysqli_query($link,"SET NAMES 'utf8'");
mysqli_query($link,"SET CHARACTER SET utf8");
mysqli_query($link,"SET CHARACTER_SET_CONNECTION=utf8");
mysqli_query($link,"SET SQL_MODE = ''");
    
15.10.2018 / 20:57