Hi i have made a function in which i am getting results from db and storing it to an array and returned an array to the function. Below is the function:
public function products() {
shopping::connection();
$get = mysqli_query($this -> connection, 'Select * from products limit 5');
$result[] = array();
while($results = mysqli_fetch_object($get)) {
$result[] = $results;
}
return $result;
}
so now this function is another file called functions.php i called this file into another file where i want to pull all the data and display on that page so i included the file and stored the function in variable. Below how i did
<?php require_once('codec/functions.php'); ?>
<?php $products = $object -> products(); ?>
Now if i do print_r($products) gives me an array of data, but when i try to retrieve the data using this function
<?php echo $product -> price; ?>
It gives me an error of
Notice: Trying to get property of non-object in C:\xampp\htdocs\Shopping\index.php on line 15
I did search on google but didn't found result that i want. Please tell me what i am doing wrong. Thanks
print_r($product). And this$result[] = array();is useless. So your first array element is empty array, which is not object.$result = array();codec/functions.php? If the contents of that file is just a bunch of normalCustom Functions, you need not add Access Modifiers (like Public) to the Functions and you also don't need to call it on any object: that is — You'd call it directly...$products = products();instead of$products = $object -> products();.... without$object->