PHP Uncaught Error: Call to a member function query() on null

All we need is an easy explanation of the problem, so here it is.

I don’t understand this error, perhaps I use in wrong way methods from the extended class.
Something about code: Image class is extending class Database. In Database class I have selectQuery() that works fine when I’m using it from Database class, but when I’m trying to call it in construct of Image class I got this error back.

error

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\strona\myAPI\Models\Database.php:24 Stack trace: #0 C:\xampp\htdocs\strona\myAPI\Models\ImageModel.php(26): Database->selectQuery(‘images’, ‘imageid = 3333’) #1 C:\xampp\htdocs\strona\myAPI\Models\ImageModel.php(117): Image->__construct(3333, 3333) #2 {main} thrown in C:\xampp\htdocs\strona\myAPI\Models\Database.php on line 24

Database.php

<?php

class Database
{
    public $con;
    public function __construct()
    {
        try {
            $this->con = new PDO('mysql:dbname=' . DB_DATABASE_NAME . ';host:' . DB_HOST, "root", "");
            $this->con->exec("set names utf8");
        } catch (PDOException $e) {
            print_r("Something went wrong: <br>" . $e->getMessage());
        }
    }

    public function selectQuery(string $where, array $selectors)
    {
        if (isset($where) && isset($selectors)) {
            $selectors = selectorsGen($selectors);
            $query = "SELECT * FROM $where  WHERE $selectors ";

            try {
                $stm = $this->con->query($query);
            } catch (PDOException $e) {
                print "Something went wrong: <br>" . $e->getMessage();
            }
            return $stm->fetch(PDO::FETCH_ASSOC);
        }
    }
//rest of database class

ImageModel.php

<?php

use Image as GlobalImage;

require "./Database.php";

class Image extends Database
{
    public $con;
    public $imageid;
    public function __construct(int $imageid, int $userid)
    {
        $selectors = array(
            "imageid" => $imageid,
            "userid" => $userid,
        );
        $where = "images";
        $data = Database::selectQuery($where, $selectors); //look here
        $this->imagestring = $data['imagestring'];
        $this->imagecomment = $data['imagecomment'];
        $this->imagedate = $data['imagedate'];
        $this->imageprivate = $data['private'];
        $this->imageorder = $data['imageorder'];
        $this->albumid = $data['albumid'];
        }
//rest of image class
}

//this call make this error
$img= new Image(3333, 3333);
echo $img->getImageComment();

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

selectQuery() isn’t a static method, it needs to be called through an object. You can use $this in the child class.

Also, the child class constructor needs to call the parent class’s constructor so that the database connection will be opened.

And then you need to assign the result of selectQuery() to the $data variable so you can access the columns.

class Image extends Database
{
    public $imageid;
    public function __construct(int $imageid, int $userid)
    {
        parent::__construct();
        $selectors = array(
            "imageid" => $imageid,
            "userid" => $userid,
        );
        $where = "images";
        $data = $this->selectQuery($where, $selectors); //look here
        $this->imagestring = $data['imagestring'];
        $this->imagecomment = $data['imagecomment'];
        $this->imagedate = $data['imagedate'];
        $this->imageprivate = $data['private'];
        $this->imageorder = $data['imageorder'];
        $this->albumid = $data['albumid'];
    }
//rest of image class
}

The child class shouldn’t declare its own $con variable, it inherits that from the parent automatically.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply