Home >Backend Development >PHP Tutorial >Introduction to usage examples of json_encode in php
In the last article, we introduced you to the detailed explanation of the json_encode() function in php, and we know that it can only be used in versions above php5.2. So today we will briefly introduce json_encode to us. The method of use is very simple and practical, and I recommend it to everyone.
1. Query the data from the database and put it in the array
$query=mysql_query($SQL); while($row = mysql_fetch_array($query)){ $xdata[]=$row['EventDate']; $ydata[]=intval($row['data']); }
2. Convert the data into json
$data_arr=array($xdata,$ydata) json_encode($data_arr);
3. AJAX call data in HTML page
$.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 yData = jsondata[1]; var namestr = jsondata[2]; }, error: function () { alert("fail!"); } });
Summary:
Believe it through studying this article Everyone has a better understanding of how to use json_encode in php. I hope it will be helpful to your work!
Related recommendations:
The above is the detailed content of Introduction to usage examples of json_encode in php. For more information, please follow other related articles on the PHP Chinese website!