Call to a member function query() on null

Here’s a new file:

//Products.php
<h1>Products</h1>
<?php

$res = IA::printAll();
if (!empty($res))
{
    $i = 0;
    foreach ($res as $item)
    {
        echo $item[$i];
        $i++;
    }
}
if (User::isLogged())
{
    $ids = IA::getIDs();
    echo "<form name='productpf' method='POST' action='index.php'>
          <select name='id'>";
            if (!empty($ids))
            {
                $a = 0;
                foreach ($ids as $item)
                {
                    echo "<option value = $item[$a]>$item[$a]</option>";
                    $a++;
                }
            }
    echo"</select>
         <input type='submit' name='addcart' value='Add To Cart' size='30'>
         </form>";
}

?>

OK, so the problem now, is that a Fatal error: Call to a member function query() on null in C:xampphtdocsInternetApplicationsClassesIA.php on line 41 is happening. So here are the two functions called on IA.php:

        //IA.php
        public function get()
        {
            $db = MySQL::getDB();
            $sql = "SELECT * FROM ia";
            $res = $db->query($sql); //------------->LINE 41<--------------
            return $res;
        }

        public static function printAll()
        {
            $p = new IA();
            $res = $p->get();
            $nrows = $res->num_rows;
            $array = array();
            for($i = 0;$i < $nrows; $i++)
            {
                $pdb = $res->fetch_object();
                $array[] = array($i => "<div style='border-style:groove' style='border:5px solid gray'>
                        <p>id: '$pdb->id' </p>
                        <p>name: '$pdb->name' </p>
                        <p>type: '$pdb->type' </p>
                        <p>description: '$pdb->description' </p>
                        <p>price: '$pdb->price'</p>
                        </div>");
            }
            $res->free();
            return $array;
        }

        public static function getIDs()
        {
            $p = new IA();
            $res = $p->get();
            $nrows = $res->num_rows;
            $array = array();
            for($i = 0;$i < $nrows; $i++)
            {
                $pdb = $res->fetch_object();
                $array[] = array($i => $pdb->id);
            }
            $res->free();
            return $array;
        }

Now the weird thing, as you can see, both functions above call get() method, and only the second one being called gives the error. In this case the second one being called is getIDs(), so that is the function that doesn’t send any output. IF I switch the order and put getIDs() in first, it works but the second one, printAll() does not, therefore, no output. Here are the last two methods from the MySQL class, both of them called by function get():

//MySQL.php
public static function getDB()
    {
        if (self::$db == NULL)
        {
            return self::$db = new self();
        }
    }

public function query($sql)
    {
        $res = $this->conn->query($sql);
        if (!$res) 
        {
            echo $this->conn->error;
        }
        return($res);    
    }


Source: sql

Leave a Reply