Home  >  Article  >  Backend Development  >  How to convert mysql data into json format in php

How to convert mysql data into json format in php

coldplay.xixi
coldplay.xixiOriginal
2020-09-07 10:33:384597browse

php method to convert mysql data into json format: first mysql test database table, and create a new [testmysql.php] file to test; then use the database to execute the query statement; then put the data into the user object; Finally, use the [json_encode] function to convert the data into json format.

How to convert mysql data into json format in php

[Related learning recommendations: php programming (video)]

php method to convert mysql data into json format:

1. First, mysql test database table

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
  `login_name` varchar(50) NOT NULL COMMENT '用户名',
  `usable` int(11) NOT NULL DEFAULT '1' COMMENT '是否可用 0:不可用;1:可用',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `last_login_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后登录时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `login_name` (`login_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户表';

2. Create a new testmysql.php file in the idea development tool to test

创建mysql连接
$link = mysql_connect('xxx:3306', 'root', 'xxx');
if (!$link) {
    echo "fail";
}

3. Select the database, execute the query statement, and $result receives the returned Data content

mysql_select_db("study");
$result = mysql_query("select * from t_user");

4. Define a user object

class User
{
   public $login_name;
   public $usable;
   public $create_time;
}

5. Loop through the data and put it into the user object

$data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $user = new User();
    $user->login_name = $row["login_name"];
    $user->usable = $row["usable"];
    $user->create_time = $row["create_time"];
    $data[] = $user;
}

6. Use the json_encode function to convert the array data into json format, and then echo to print it out.

$json = json_encode($data);echo "{" . '"user"' . ":" . $json . "}";

7. Right-click and select Run to see the console. After running, the results are printed

{"user":[{"login_name":"aa1","usable":"2","create_time":"2017-08-29 18:21:35"},{"login_name":"aa","usable":"1","create_time":"2017-08-29 18:22:30"}]}

The information is successfully read from mysql and converted into json format for printing

The complete sample code is as follows:

 

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How to convert mysql data into json format 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