Ok so I have been trawling Internet for hours trying to resolve this issue but I am still at a complete lost, so would very much welcome some help or point in the right direction.
I have a MySQL query where I calculate the average of a column which is all fine. It’s in displaying it is where I am having problems.
I've dumped the $results variable and it returns the following:
array(1) { [0]=> object(stdClass)#1826 (2) { ["page_id"]=> string(1) "5" ["avg(r_location)"]=> string(6) "4.0000" } }
The error I get also from the echo $row... is:
Fatal error: Cannot use object of type stdClass as array
My code is below
function returnResults() {
$postid = get_the_ID();
global $wpdb;
$results = $wpdb->get_results(" SELECT page_id, avg(r_location) FROM wp_ratings WHERE page_id = '$postid' ");
var_dump($results);
foreach ($results as $row) {
echo $row['avg(r_location)'];
}
}