tristangemus Posted January 24, 2014 Share Posted January 24, 2014 (edited) Hello, I am working through on a site and keep getting this error. I have searched similar problems but cannot find the answer. Hopefully somebody could help! "Catchable fatal error: Object of class mysqli could not be converted to string in \app\models\m_products.php on line 16" <?php /* Products Class Handles all tasks related to retrieving and displaying products */ class Products { private $Database; private $db_table = 'products'; function __construct() { global $Database; $this->$Database = $Database; } /* Getters & Setters */ // Retrieve information from the database public function get($id = NULL) { $data = array(); if (is_array($id)) { // get products on array of ids } else if ($id != NULL) { // get one specific product if ($stmt = $this->Database->prepare("SELECT $this->db_table.id, $this->db_table.name, $this->db_table.description, $this->db_table.price, $this->db_table.image, categories.name AS category_name FROM $this->db_table, categories WHERE $this->db_table.id = ? AND $this->db_table.category_id = categories.id")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($prod_id, $prod_name, $prod_description, $prod_price, $prod_image, $cat_name); $stmt->fetch(); if ($stmt->num_rows > 0) { $data = array('id' => $prod_id, 'name' => $prod_name, 'description' => $prod_description, 'price' => $prod_price, 'image' => $prod_image, 'category_name' => $cat_name); } $stmt->close(); } } else { // display all products } return $data; } } Edited January 24, 2014 by tristangemus Quote Link to comment https://forums.phpfreaks.com/topic/285645-catchable-fatal-error-object-of-class-mysqli-could-not-be-converted-to-string/ Share on other sites More sharing options...
boompa Posted January 24, 2014 Share Posted January 24, 2014 (edited) You should change this $this->$Database = $Database; to $this->Database = $Database, although I would question why you're using a global instead of just passing in the $Database variable like this: function __construct($database) { $this->Database = $database; } And you create the object with $products = new Products($Database); Global variables as a rule are a bad thing. Edited January 24, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/285645-catchable-fatal-error-object-of-class-mysqli-could-not-be-converted-to-string/#findComment-1466424 Share on other sites More sharing options...
tristangemus Posted January 24, 2014 Author Share Posted January 24, 2014 You should change this $this->$Database = $Database; to $this->Database = $Database, although I would question why you're using a global instead of just passing in the $Database variable like this: function __construct($database) { $this->Database = $database } And you create the object with $products = new Products($Database) Global variables as a rule are a bad thing. Thanks for the response. I can't believe I missed that ($this->Database etc..)\ And I'm working through a tutorial, still getting the hang of best practices for OOP. Thanks, Tristan. Quote Link to comment https://forums.phpfreaks.com/topic/285645-catchable-fatal-error-object-of-class-mysqli-could-not-be-converted-to-string/#findComment-1466431 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.