Jump to content

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


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;
}
}
Edited by tristangemus

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 by boompa

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.