Php simply queries the database and returns json data, while php 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']. '</br>'; $ 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.