Jump to content

Catchable fatal error: Object of class mysqli could not be converted to string


tristangemus

Recommended Posts

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;
}
}

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.