Home  >  Article  >  Backend Development  >  A brief discussion on json_encode usage_PHP tutorial

A brief discussion on json_encode usage_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:04:561028browse

A brief discussion on the usage of json_encode

1. The data queried from the database is placed in an array

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

The code is as follows:


$data_arr=array($xdata,$ydata)
json_encode($data_arr);

3. AJAX call data in HTML page

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 yData = jsondata[1];
var namestr = jsondata[2];
},
error: function () {
alert("fail!");
}
});

The above is all about the usage of json_encode. I hope it will be helpful to everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963983.htmlTechArticleA brief discussion on the usage of json_encode 1. The data queried from the database is placed in the array with the following code: $query= mysql_query($SQL); while($row = mysql_fetch_array($query)){ $xdata[]=$row['Eve...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn