The usage of json_encode and the tutorial of json_encode_PHP

Source: Internet
Author: User
Let's talk about json_encode usage and json_encode. Let's talk about json_encode usage. let's talk about json_encode1. copy the code from the database data in the array: $ querymysql_query ($ SQL); while ($ rowmysql_fetch_a: Talk about json_encode usage;

1. store the data queried from the database 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 on the 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 json_encode usage method. I hope it will be helpful to you.

Lifecycle 1. the code of the data queried from the database in the array is as follows: $ query = mysql_query ($ SQL); while ($ row = mysql_fetch_a...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.