Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Was making a small shop app, and got this error. To me it looks strange, because the query function in php is in-built and in my case it should work properly. But it doesn't.

The error text.

Fatal error:  Uncaught Error: Call to a member function query() on null in C:\Program Files\Ampps\www\MCV for nordcurrent\app\models\model_product_list.php:45
Stack trace:
#0 C:\Program Files\Ampps\www\MCV for nordcurrent\index.php(5): Card->get_cards()
#1 {main}
  thrown in C:\Program Files\Ampps\www\MCV for nordcurrent\app\models\model_product_list.php on line 45


Code the is in question.

PHP
  1  <?php
  2  
  3  /* Card class - proccesses all the infromation displayed in the cards */
  4  
  5  class Card
  6  {
  7      private $Database;
  8      private $db_table = 'card_table';
  9  
 10      function __constructor()
 11      {
 12          global $Database;
 13          $this->Database = $Database;
 14      }
 15  
 16      //Setting / Getting cards from the database
 17  
 18      //Return and array with card information
 19      public function get_cards($id = NULL)
 20      {
 21          $data = array();
 22          if($id != null)
 23          {
 24              //get specific card
 25              if($stmt = $this->Database->prepare("SELECT id, name FROM " . 
 26              $this->db_table . " WHERE id = ?" ))
 27              {
 28                  $stmt->bind_param("i", $id);
 29                  $stmt->execute();
 30                  $stmt->store_result();
 31  
 32                  $stmt->bind_result($card_id, $card_name);
 33                  $stmt->fetch();
 34  
 35                  if($stmt->num_rows > 0)
 36                  {
 37                      $data = array('id' => $card_id, 'name' => $card_name);
 38                  }
 39                  $stmt->close();
 40              }
 41          }
 42          else
 43          {
 44              //get all cards
 45             (The 45th line where the error is coming from) 
 46             if($result = $this->Database->query("SELECT * FROM " . $this->db_table . "
 47              ORDER BY name"))
 48              {
 49                  if($result->num_rows > 0)
 50                  {
 51                      while($row = $result->fetch_array())
 52                      {
 53                          $data[] = array('id' => $row['id'], 'name' => $row['name']);
 54                      }
 55                  }
 56              }
 57          }
 58          return $data;
 59      }
 60  
 61  
 62  }


What I have tried:

This is the n'th time when I've use this same code snippet to generate a request to a database. But this is the first time I'm getting a null error.
Posted
Updated 3-Feb-21 5:45am
v2
Comments
Richard MacCutchan 3-Feb-21 11:48am    
I cannot see anywhere that you initialise $Database to actually refer to something. As it stands it is an empty variable.
Demetri K.2 4-Feb-21 9:56am    
//Connect to database
$server = 'localhost';
$user = 'root';
$pass = 'mysql';
$db = 'nc_task';
$Database = new mysqli($server, $user, $pass, $db);

the database was declared in a separate file...
Richard MacCutchan 4-Feb-21 10:10am    
Yes, but that is not the same variable as in your card class.
Demetri K.2 4-Feb-21 10:13am    
Shouldn't the global $Database be able to get the info from the init file? Where the $Database variable is declared.
Richard MacCutchan 4-Feb-21 10:14am    
See PHP: Variable scope - Manual[^] for PHP scope rules, and the use of global.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900