I’m trying to retrieve a sum of a column and parse it with JSON.
To retrieve the sum I use:
SELECT SUM(price) FROM `Fuelconsumption` WHERE `date` like '%09-2012%'
When I use this command into my SQL Server directly, i get a figure result, when I try to parse it through JSON it doesn’t work.
Here is my JSON and PHP Code:
case 'getstats':
$query="SELECT SUM(price) FROM `Fuelconsumption` WHERE `date` like '%09-2012%'";
$result = mysql_query($query);
$json = array();
while($row = mysql_fetch_array($result))
{
$json['price']=$row['price'];
}
print json_encode($json);
mysql_close();
break;
And this is my JS:
xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsontext = xmlhttp.responseText;
var json = JSON.parse(jsontext);
console.log(jsontext)
}
}
xmlhttp.open("GET", "mysql.php?p=getstats" + "&date=09-2012", true);
xmlhttp.send();
Try
instead of
Also, why are you using normal Javascript to create Ajax requests instead of jQuery while your name is jQuerybeast? 😛