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...