Php simply queries the json data returned by the database, and php database returns json_PHP tutorial

Source: Internet
Author: User
Tags php database
Php simply queries the database and returns json data, while php returns json data. Php simply queries the json data returned by the database. Code 1 of the sample code returned by the php database: Set the returned json format header (content-type: applicationjson; charsetutf8 ); connect to php to query the database and return json data. The php database returns json data.

Sample Code 1:

// Set the returned json data header ('content-type: application/json; charset = utf8'); // connect to the database $ link = mysql_connect ("localhost ", "root", "root") or die ("Unable to connect to the MySQL! "); Mysql_query (" set names 'utf8' "); mysql_select_db (" jilinwula ", $ link) or die (" Unable to connect to the MySQL! "); // Get the paging parameter $ page = 0; $ pageSize = 3; if (! Is_null ($ _ GET ["page"]) {$ page = $ _ GET ["page"];} if (! Is_null ($ _ GET ["pageSize"]) {$ pageSize = $ _ GET ["pageSize"];} // query data to the array $ result = mysql_query ("select username, password from userinfo limit ". $ page. ",". $ pageSize. ""); $ results = array (); while ($ row = mysql_fetch_assoc ($ result) {$ results [] = $ row ;} // Convert the array to json format echo json_encode ($ results); // close the connection mysql_free_result ($ result); mysql_close ($ link );

Sample Code 2:

<? Php // SQL statement to be executed // Single $ SQL = "select id, name from tbl_user where id = 1 "; // multiple data records // $ SQL = "select id, name from tbl_user"; // call conn. php file for database operation require ('Conn. php '); // The message indicating successful operation is displayed. note: $ result exists in conn. in the php file, if ($ result) {// $ array = mysql_fetch_array ($ result, MYSQL_ASSOC);/* dataset $ users = array (); $ I = 0; while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) {echo $ row ['id']. '-----------'. $ row ['name'].'
'; $ Users [$ I] = $ row; $ I ++;} echo json_encode (array ('datalist' => $ users )); * // * single data entry */$ row = mysql_fetch_row ($ result, MYSQL_ASSOC); echo json_encode (array ('jsonobj '=> $ row ));} mysql_free_result ($ result); // release result mysql_close (); // close the connection?>

The above is all the content of this article. I hope you will like it.

Example code 1: // Set the returned json data header ('content-type: application/json; charset = utf8'); // connect...

Related Article

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.