Your JSON is invalid. Object keys in JSON must be quoted with double-quotes, eg: {id: "ID", type: "string"}
should be {"id":"ID","type":"string"}
. By manually encoding your data as JSON, you are missing out on all the benefits of having the json_encode
function available to you (it actually does next to nothing in your current setup). If you construct your data in arrays, json_encode
will happily take care of the tedious parts of encoding your data for you:
$myData = array(
'cols' => array(
array('id' => 'ID', 'type' => 'string'),
array('id' => 'Poles', 'type' => 'number'),
array('id' => 'Victories', 'type' => 'number'),
array('id' => 'Team', 'type' => 'string')
),
'rows' => array();
);
while ($red = $db->getResult()->fetch_object()) {
$myData['rows'][] = array('c' => array(
array('v' => $red->name),
array('v' => $red->poles),
array('v' => $red->victories),
array('v' => $red->teamName)
));
}
echo json_encode($myData, JSON_NUMERIC_CHECK);
For reference, PHP's associative arrays are encoded as javascript objects, non-associative arrays are encoded as javascript arrays. JSON_NUMERIC_CHECK
scans the data for numbers to make sure they are encoded as numbers and not strings (this helps because some databases will output numbers as strings, MySQL being the primary culprit).