PHP Simple implementation Query database return JSON data, PHP database returned json_php tutorial

Source: Internet
Author: User
Tags php database

PHP Simple implementation Query database return JSON data, PHP database return JSON


Example code one:

Sets the return JSON format Data header (' Content-type:application/json;charset=utf8 ');//Connect 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 paging parameter $page = 0; $pageSize = 3;if (!is_null ($_get["page")) {$page = $_get["page"];} if (!is_null ($_get["PageSize")) {$pageSize = $_get["PageSize"];} Query the data into the array $result = mysql_query ("Select Username,password from UserInfo limit". $page. ", ". $pageSize. ""); $results = Array (), while ($row = Mysql_fetch_assoc ($result)) {$results [] = $row;} Turn the array into JSON format echo json_encode ($results);//close Connection mysql_free_result ($result); Mysql_close ($link);

Example code two:

<?php//need to execute SQL statement//single $sql= "Select Id,name from Tbl_user where id=1";//Multiple data//$sql = "Select Id,name from Tbl_user";// Call the conn.php file for database operation require (' conn.php '); Prompt Operation success Information, note: $result exists in the conn.php file, is called out if ($result) {//$array =mysql_fetch_array ($result, MYSQL_ASSOC);   /* Data set $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 */$row =mysql_fetch_row ($result, MYSQL_ASSOC); echo json_encode (Array (' jsonobj ' = $row));} Mysql_free_result ($result);//release result mysql_close ();//close Connection?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/985277.html www.bkjia.com true http://www.bkjia.com/PHPjc/985277.html techarticle PHP Simple implementation Query database return JSON data, PHP database return JSON sample code one://Set return JSON format 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.