Domagoj Posted November 25, 2020 Report Share Posted November 25, 2020 Hello im getting this error: Fatal error: Uncaught Error: Call to a member function prepare() on null in D:\wamp\www\shop\app\models\m_categories.php on line 35 here is my code for m_categories.php <?php /* Categories Handles all tasks related to retrieving and displaying categories */ class Categories { private $Database; private $db_table='categories'; function __construct() { global $Database; $this->Database=$Database; } /* Settting/Getting categories from database */ /** * Return an array with category information * * @access public * @param int(optional) * @return array */ public function get_categories($id=NULL) { $data=array(); if($id != NULL) { // get specific category if($stmt=$this->Database->prepare("SELECT id, name FROM" . $this->db_table . " WHERE id=? LIMIT 1")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($cat_id,$cat_name); $stmt->fetch(); if($stmt->num_rows > 0) { $data=array('id'=>$cat_id, 'name'=>$cat_name); } $stmt->close(); } } else { // get all categories if($result=$this->Database->query("SELECT * FROM" . $this->db_table . "ORDER BY name")) { if($result->num_rows>0) { while($row=$result->fetch_array()) { $data[]=array('id'=>$row['id'], 'name'=>$row['name']); } } } } return $data; } } ?> and init.php <?php /* INIT Basic configuration settings */ // connect to database $server='localhost'; $user='root'; $pass=''; $db='shop'; $database=new mysqli($server,$user,$pass,$db); // error reporting mysqli_report(MYSQLI_REPORT_ERROR); ini_set('display_errors', 1); // setup constants define('SITE_NAME', 'Online Store'); define('SITE_PATH','http://shop/'); define('IMAGE_PATH','http://shop/resources/images/'); // include objects include('app/models/m_template.php'); include('app/models/m_categories.php'); // create objects $template= new Template(); $categories= new Categories(); session_start(); and index.php: <?php include('app/init.php'); echo "<pre>"; print_r($categories->get_categories(3)); echo "</pre>"; /*$template->set_data('header','hello'); $template->set_alert('alert!'); $template->load('app/views/v_public_home.php', 'Welcome!');*/ ?> Thank you in advance. Quote Link to comment Share on other sites More sharing options...
administrator Posted December 22, 2020 Report Share Posted December 22, 2020 Sounds like you are not properly declaring a variable. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.