user379888
user379888

Reputation:

Call to a member function query() on null in PHP

I am getting below error when I run my PHP code.

Call to a member function query() on null

Below is the code,

function fetch_url($url_code) {
    $query = "SELECT * FROM `urls` WHERE `url_code` = ? ";

    $result = $this->db->query($query, array($url_code));
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

Please guide me how to remove this error.

Upvotes: 0

Views: 11892

Answers (5)

Gagandeep cheema
Gagandeep cheema

Reputation: 1

Yes use this lines of code to get rid of the error Call to a member function query() on null in PHP I used it in my database class and it's working fine !

Upvotes: -1

Gagandeep cheema
Gagandeep cheema

Reputation: 1

class Database {

public $host=DB_HOST;           // Specify localhost
public $username=DB_USER;       //Specify username
public $password=DB_PASSWORD;   // specify password
public $dbname=DB_NAME;         // specify database name

public $conn ;
public $error;

/*

* Class Constructor 

*/

Public function __constructor() {


    // Call Connect Function

    $this->connect();

}

/*
*    Connector
*/

Public function connect() {

    //* Creating mySqli Class Object * //

 $this->conn  = new mysqli($this->host,$this->username,$this->password,$this->dbname);  

  if (!$this->conn) {

      $this->error = "Connection failed :" .$conn->connect_error ;
      return false;

                    }

                            }

    /*
    * Select function
    */

    public function select($query) {

        /*  to avoid call to member function query() on Null Error 

        Use this 2 lines of code */

         if ($this->conn === null) {
            $this->connect();
            }           

        $results = $this->conn->query($query) or die($this->conn->error.__LINE__);

    /* If there is atleast one row in the table Condition is true / Return table Rows as per your sql query */

    if($results->num_rows > 0) {  

                     return $results;

    }

                     else {

                         return false;

                          }
         } //Select Function Ends;


    } //Class ends here ..

Upvotes: 0

Gagandeep cheema
Gagandeep cheema

Reputation: 1

class Database {

public $host='localhost';  // Your Local Computer
public $username='root';  // Your PhpMyAdmin Username
public $password='';  // Your PhpMyAdmin Password
public $dbname='blog'; // Your database Name


// Declaring Variables 

public $conn ;
public $error;

/*
* Class Constructor 
*/

Public function __constructor() {


    // Call Connect Function

    $this->connect();

}

/*
*    Connector
*/

Public function connect() {

    //* Creating mySqli Class Object * //

 $this->conn  = new mysqli($this->host,$this->username,$this->password,$this->dbname);  

  if (!$this->conn) {

      $this->error = "Connection failed :" .$conn->connect_error ;
      return false;

                    }

                            }


    /*
    * Select function
    */

    public function select($query) {

        // these 2 lines of code help to resolve error "Call to a member function query() on null " //


         if ($this->conn === null) {
            $this->connect();
            } 


        $results = $this->conn->query($query) or die($mysqli->error.__LINE__);


    /* If there is atleast one records in the table based on Sql Query ! Condition is true / Return table Records*/

    if($results->num_rows > 0) {  

                     return $results;

                 }

                     else {

                         return false;

                          }

Upvotes: -2

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

this->db->query function need only SQL Code. not like PDO.

Make sure Library is loaded

In config/autoload.php add this $autoload['libraries'] = array('database');


Try this

function fetch_url($url_code) {

    $query = $this->db->query("SELECT * FROM urls WHERE url_code = '$url_code' ");
    $result = $query->result_array(); # setting array to objective 
    $count = count($result); # get count

    if (!empty($count)) {
        return $result;
    } 
    else {
        return false;
    }
}

Upvotes: 2

ahuemmer
ahuemmer

Reputation: 2057

Your $this->db is null. Please make sure, it is initialized properly before calling query. Here is a very basic example for MySQL (you will have to adapt it to your DB system):

function init() {
    $this->db = new mysqli('host', 'user', 'password', 'database');
    if (!$this->db) {
         die('MySQL Connection Error (' . mysqli_connect_errno() . ') '
             . mysqli_connect_error());
    }
}

function fetch_url($url_code) {

    $sql = "SELECT * FROM urls WHERE url_code = '$url_code' ";

    if ($this->db === null) {
        $this->init();
    }

    $query = $this->db->query($sql);
    $result = $query->result_array(); # setting array to objective 
    $count = count($result); # get count

    if (!empty($count)) {
        return $result;
    } 
    else {
        return false;
    }
}

Upvotes: 2

Related Questions