Let me preface this with I know this question has been asked plenty of times on here (ie here among other pages) and I've trawled through each of the answers and tried the solutions but keep getting an empty json array.
I've got a mysql query as per below:
$myQuery = $wpdb->get_results('SELECT player_name, player_team,
SUM(ruckContests) AS contests FROM afl_master
WHERE player_team = "COLL" AND year = 2019 AND ruckContests > 0
GROUP BY player_id ORDER BY contests DESC LIMIT 2');
The output to this query is as follows:
Array ( [0] => stdClass Object ( [player_name] => Brodie Grundy [player_team] => COLL [contests] => 661 ) [1] => stdClass Object ( [player_name] => Mason Cox [player_team] => COLL [contests] => 51 ) )
What I want to do is convert this to the following json object in php:
{
player_name: "Brodie Grundy",
player_team: "COLL",
contests: 661
},
{
player_name: "Mason Cox",
player_team: "COLL",
contests: 51
}
I've tried mysql_fetch_array()
as well as fetch_assoc()
using the method here but to no avail. I'd appreciate any help available.
This simple solution recommended in the comments worked:
json_encode($myQuery)