How to query data in php and convert the results to json format

PHPz
Release: 2023-04-04 17:30:02
Original
1270 people have browsed it

With the development of Internet technology, the need to output data query results in JSON format is becoming more and more common. PHP is a scripting language that can easily interact with databases such as MySQL to implement data queries and convert the results into JSON format. This article will introduce how to use PHP to query data and convert the results into JSON format for output.

1. Connect to the database

In PHP, use the mysqli_connect function to connect to the MySQL database. The following parameters need to be provided:

1. Host address
2. User name
3. Password
4. Database name

$host='localhost'; //主机地址
$username='root';  //用户名
$password='123456'; //密码
$database='test';  //数据库名

$mysqli=mysqli_connect($host,$username,$password,$database); //连接数据库
if(mysqli_connect_errno()) {  
    echo "无法连接数据库:". mysqli_connect_error();  
    exit;  
}
Copy after login

2. Query data

After the connection is successful, you can start querying data. Use the mysqli_query function to execute SQL statements and return a result set.

$sql = "SELECT * FROM `user`";
$result = mysqli_query($mysqli,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($mysqli));
    exit();
}
Copy after login

In the above code, all data in the "user" table is queried and stored in the $result variable. If there is an error in the query, an error message will be returned.

3. Convert the result to JSON

PHP provides the json_encode function to convert arrays or objects into JSON format. Therefore, in the query results, we can first save the obtained data in an array and use json_encode to convert it to JSON format. It should be noted that since Chinese is not supported in JSON, you need to pass the parameter JSON_UNESCAPED_UNICODE to the json_encode function to retain Chinese.

The following is the code to convert the query results into JSON format:

$data=array(); //声明一个数组变量用于存放数据
while($row=mysqli_fetch_assoc($result)){ //将查询结果保存到数组中
    $data[]=$row;
}

echo json_encode($data, JSON_UNESCAPED_UNICODE); //将数组转为JSON格式
Copy after login

4. Complete code

Combine the above codes to get a complete query and convert the data into Code in JSON format.

$host='localhost'; //主机地址
$username='root';  //用户名
$password='123456'; //密码
$database='test';  //数据库名

$mysqli=mysqli_connect($host,$username,$password,$database); //连接数据库
if(mysqli_connect_errno()) {  
    echo "无法连接数据库:". mysqli_connect_error();  
    exit;  
}

$sql = "SELECT * FROM `user`";
$result = mysqli_query($mysqli,$sql);
if (!$result) {
    printf("Error: %s\n", mysqli_error($mysqli));
    exit();
}

$data=array(); //声明一个数组变量用于存放数据
while($row=mysqli_fetch_assoc($result)){ //将查询结果保存到数组中
    $data[]=$row;
}

echo json_encode($data, JSON_UNESCAPED_UNICODE); //将数组转为JSON格式
Copy after login

5. Summary

This article introduces how to use PHP to query data and convert the results into JSON format for output. By connecting to the database, querying data, and using json_encode to convert the results into JSON format, the data can be easily output to relevant applications in a unified format, improving data transmission efficiency and application development efficiency.

The above is the detailed content of How to query data in php and convert the results to json format. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template