Home >Backend Development >PHP Tutorial >A brief discussion on json_encode usage, a brief discussion on json_encode_PHP tutorial
1. The data queried from the database is placed in an array
Copy code The code is as follows:
$query=mysql_query($SQL);
while($row = mysql_fetch_array($query)){
$xdata[]=$row['EventDate'];
$ydata[]=intval($row['data']);
}
2. Convert data to json
Copy code The code is as follows:
$data_arr=array($xdata,$ydata)
json_encode($data_arr);
3. AJAX call data in HTML page
Copy code The code is as follows:
$.ajax({
Type: "Get",
url: "columndata.php?r=" + Math.floor(Math.random() * 1000 + 1),
data: { 'BeginTime': "" + beginTime + "", "EndTime": "" + endTime + "" , "keyword": "" + keyword + "" },
dataType: "text",
global: false,
async: false,
Success: function (strReult) {
If (strReult == "-1") { alert("fail!"); return; }
var jsondata = eval("(" + strReult + ")");
var xData = jsondata[0]; var
var yData = jsondata[1];
var namestr = jsondata[2];
},
error: function () {
alert("fail!");
}
});