Pass mysql class to mysqli error in query

Asked

Viewed 71 times

0

I have a problem to pass the consultation (this->Query($sql)) and the rest to mysqli. I don’t know how to proceed

MISTAKES:

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);
    }
}
?>

As 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();

?>

  • 1

    It would be interesting to have some of the mistakes that happen so that we can get from there. So it will be amis easy to understand each mistake and bring a solution.

  • You are right, here the errors that happen 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 @Erloncharles

  • Put the code as you call this class.

  • @rray &#xA;<?php&#xA; # configuration for database&#xA; $_config['database']['hostname'] = "localhost";&#xA; $_config['database']['username'] = "root";&#xA; $_config['database']['password'] = "xxx";&#xA; $_config['database']['database'] = "banco";&#xA; &#xA; # connect the database server&#xA; $link = new mysqldb();&#xA; $link->connect($_config['database']);&#xA; $link->selectdb($_config['database']['database']);&#xA; $link->query("SET NAMES 'utf8'");&#xA; &#xA; @session_start();&#xA;?>

  • @rray, used this way in php5, here locally, no problem, what I wanted to put in a server here on the other side that used php7 ai needed to migrate mysql strings to mysqli, that’s where the problems occur, in the errors the connection seems ok only the query that of the error, but I also don’t know how to confirm

  • Do the following: change: $this->query = mysqli_query($this->link, $sql); for $this->query = mysqli_query($this->link, $sql) or die(mysqli_error($this->link)); see if another error message appears.

  • @rray now leaves the screen blank without error, in apache log continues mysqli_query() expects Parameter 1 to be mysqli, null Given in

  • So the connection is not valid, check this, see the return of connect()

  • @rray how? Return connect(); ? so gave error. do not know much ><

Show 4 more comments

1 answer

0

Cara I think the problem is in your configuration file that connects to the bank that is still in the old model.

$link = new mysqldb();

Try this way below with mysqli, always use and never had 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 = ''");
  • same error mysqli_error() expects Parameter 1 to be mysqli, null Given in mysqli_query() expects Parameter 1 to be mysqli, null Given in

  • @Weliton tested here with the excerpt I said and it worked. I noticed that you forgot to declare var $query; in the class mysqldb. Check if this is it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.