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 ';