PHP database query results output as JSON format simple instance, query results json_php Tutorial

Source: Internet
Author: User
Tags php database

PHP to output database query results as a simple JSON format instance, query results JSON


include/conn.php for database link file, no online search

<?php include './include/conn.php '; Database link File $sql_notice = mysql_query (' SELECT * from gg_notice where enable = "1" limit 0,10 '); $notice = Mysql_fetch_array ($s Ql_notice, Mysql_assoc);p Rint_r ($notice);? ><title>Tutorial provided by the first PHP network--generating JSON format for data read from the database</title>
 
  
 
 

Note the structural differences between the two methods of generating an array of objects

<?phpecho '

Fayi

'///Suppose the following array is generated from the data we read from the database $jarr=array (' Total ' =>239, ' Row ' =>array (' code ' = ' 001 ', ' name ' = = ' China ', ' addr ' = ' Address one ', ' col4 ' = ' col4 data ', Array (' Code ' = ' 002 ', ' name ' = ' = ' Name 2 ', ' addr ' = ') Address (' col4 ' = ' col4 data ')) ;//Law one: $jobj =new stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, Since the data after Json_decode is stored as an array of objects,//So we generate the data to be stored in the object foreach ($jarr as $key + $value) {$jobj $key = $value;} Print_r ($jobj);//Print the object after passing the property Echo ' uses $jobj->row[0][\ ' code\ '] to output the array element: '. $jobj->row[0][' code '. '
'; Echo ' encoded JSON string: '. Json_encode ($jobj). '
';//print the encoded JSON string echo ';//Law two: Echo '

Law II

'; Echo ' encoded JSON string: '; echo $str =json_encode ($jarr);//JSON-encode the array echo '

http://www.bkjia.com/PHPjc/981354.html www.bkjia.com true http://www.bkjia.com/PHPjc/981354.html techarticle PHP in the database query results output as a simple JSON format instance, the query results JSON include/conn.php as a database link file, will not be searched online php include './include/conn.php ';

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