Why wont this work: Trying to get property of non-object

Hi,

I really don’t understand why this does not work…


        $prod_model = new Product();
        $products = $prod_model->selectByID(1);
        $items = mysql_fetch_object($products);
        echo $items->name; //I get error here

This line:


$prod_model = new Product();

Just refers to this class:



class Product{

    public function selectByID($ID){

        $query = "SELECT * FROM tbl_item WHERE deleted = 0 AND ID = ".$ID;
        return mysql_query($query) or die(mysql_error());
    }
}

The specific record exists in the database, i have even tried using :


 $items = mysql_fetch_array($products);

Only difference here is that it shows nothing at all…

Can anyone please help me…

Thanks again

Have you tried running your query in the database?

if you echo $query; and copy that code into PhpMyadmin, does it give you an error? Your php code looks fine to me. I’m assuming you already opened up a database connection earlier in the script.

  • First check the record deleted = 0
  • Second do you have a field named ‘name’ in the table?
  • Try to echo the query and run it outside PHP (mysql client or phpmyadmin) whether it gives you the record.