Home >Backend Development >PHP Problem >How to read database and convert json data in php
The implementation method of php reading database and converting json data: first connect to the database and read the database; then after reading the database, directly convert the data into an array for display; finally convert it to JSON through "json_encode" .
Recommended: "PHP Video Tutorial"
Code for PHP to read database records and convert them to JSON (SQL statement of API interface)
In order to provide the API interface, we often convert the data into an array after reading the database, and convert it to JSON through json_encode to meet the usage needs. Now paste the code as follows:
Read one record, convert it into an array and output JSON
include("../../db/conn.php");//数据库连接; echo "<pre class="brush:php;toolbar:false">"; //数据库读取后,直接转换为数组显示; $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales WHERE salesid=44"; $results = mysqli_query($con, $sql); $rows = mysqli_fetch_assoc($results); foreach ($rows as $key => $v) { $res[$key] = $v; } echo json_encode($res);
Read N records, convert it into a multi-dimensional array and output JSON (first way of writing)
//数据库读取后,直接转换为数组显示; $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales"; $results = mysqli_query($con, $sql); $data = array();//初始化数组; class Alteration { public $fromstore; public $fromsaler; public $salenum; public $totalprice; } while ($row = mysqli_fetch_assoc($results)) { $alter = new Alteration();//实例化对象; $alter->fromstore = $row['fromstore']; $alter->fromsaler = $row['fromsaler']; $alter->salenum = $row['salenum']; $alter->totalprice = $row['totalprice']; $data[] = $alter; } echo json_encode($data);
Read N records, convert them into multi-dimensional arrays and output JSON (the second way of writing)
$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales"; $results = mysqli_query($con, $sql); while ($rows = mysqli_fetch_assoc($results)) { $res[] = $rows; } //$res = str_replace('[', '{', json_encode($res)); //$res = str_replace(']', '}', $res); print_r($res);
4. Read N records, convert them into multi-dimensional arrays and output JSON (the second way of writing) Three writing methods), suitable for obtaining all records
$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales"; $results = mysqli_query($con, $sql); $rows = mysqli_fetch_all($results); print_r($rows);
During the conversion process, JSON format will appear in two formats: [] and {}, and the interface for {} in actual applications is a standard interface. How to convert it?
The reason is: when array is a continuous array starting from 0, the result of json_encode is a string enclosed by []; and when array is an array that does not start from 0 or is not continuous , the result of json_encode is a string in key-value pattern enclosed by {}.
$sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales"; $results = mysqli_query($con, $sql); $rows = mysqli_fetch_all($results); $rows = str_replace('[', '{', json_encode($rows)); $rows = str_replace(']', '}', $rows); echo json_encode($rows);
The above is the detailed content of How to read database and convert json data in php. For more information, please follow other related articles on the PHP Chinese website!