Home >Backend Development >PHP Problem >How to read database and convert json data in php

How to read database and convert json data in php

藏色散人
藏色散人Original
2020-08-12 09:08:523876browse

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

How to read database and convert json data in php

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[&#39;fromstore&#39;];
$alter->fromsaler = $row[&#39;fromsaler&#39;];
$alter->salenum = $row[&#39;salenum&#39;];
$alter->totalprice = $row[&#39;totalprice&#39;];
$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(&#39;[&#39;, &#39;{&#39;, json_encode($res));
//$res = str_replace(&#39;]&#39;, &#39;}&#39;, $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(&#39;[&#39;, &#39;{&#39;, json_encode($rows));
$rows = str_replace(&#39;]&#39;, &#39;}&#39;, $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn