Ashish Jat
Ashish Jat

Reputation: 5

How to convert object to MySQLi in this below code?

Hi I am trying to replace all old mysql query in this CMS to mysqli for PHP 7. But I am getting this below error on every page.

PHP Warning: mysqli_query() expects parameter 1 to be mysqli, object given in ..

In this CMS a db class file is included on every page but I can't convert the object to mysqli for the mysqli_query

Code For DB.php

class db {

    var $query;
    var $db;
    var $queryArray = array();
    var $showError = true;

    function __construct() {
        global $glob;

        $this->db = mysqli_connect($glob['dbhost'], $glob['dbusername'], $glob['dbpassword']) or die(mysqli_error());
        if (!$this->db) die($this->debug(true));

        $selectdb = mysqli_select_db($this->db,$glob['dbdatabase']);
        if (!$selectdb) die ($this->debug());       

    }

Code For the page which I am trying to open

<?php
        $instance = new db();
    $query = mysqli_query($instance,"SELECT * FROM adam_docs WHERE doc_id = '24' ORDER BY doc_name ASC");

    //echo $query; exit;
    $num_row = mysqli_num_rows($query);
    if($num_row > 0){
    $results = mysqli_fetch_array($query);
  ?>

Upvotes: 0

Views: 106

Answers (1)

Dharman
Dharman

Reputation: 33238

Your db class is not an instance of MySQLi. It does not inherit, instead it has a public property called $db. You should use this property in mysqli_query. However the keyword var should be replaced with public.

$query = mysqli_query($instance->db, "SELECT * FROM adam_docs WHERE doc_id = '24' ORDER BY doc_name ASC");

Upvotes: 1

Related Questions