PHP PDO reads MariaDB data and converts it to json

tags: Think PHP  SQL  mysql  database

Data read by pdo

The pdo query returns an object, and it is impossible to parse the query directly. . Only output line by line
$query = c o n n > q u e r y ( conn->query( sql)->fetchALL() returns an array, the direct output is Array

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=$charset", $dbuser, $dbpass);
 $sql = "SELECT * FROM $tbname";
 if ( $query = $conn->query($sql) )
 {
     echo "<p>Number Name Start Time End Time</p>";
     foreach ($query as $row)
     {
         echo $row['activity_no'] .'      ' ;
         echo $row['activity_name'] .'      ' ;
         echo $row['activity_begin_time'] .'      ' ;
         echo $row['activity_end_time'] .'      ' ;
         echo $row['activity_location'] ;
         echo $row['activity_content'] . "<br>";
     }
   }
 Number Name Start time End time

 1 Thunderstorm 2020-06-12 20:59:56 2020-06-15 20:59:58
 2 Brainstorm 2020-06-12 21:01:04 2020-06-13 21:01:05
 3 points
 4 Poetry recitation
 6 The most dazzling national style Arctic dance

Convert json

I don't know why I think of this format, the front end likes this wow. . . .

if ( $query = $conn->query($sql) )
{
   $result = json_encode($query->fetchAll());
   echo $result;
}
[{"activity_no":"1","0":"1","activity_name":"\u96f7\u96e8","1":"\u96f7\u96e8","activity_begin_time":"2020-06-12 20:59:56","2":"2020-06-12 20:59:56","activity_end_time":"2020-06-15 20:59:58","3":"2020-06-15 20:59:58","activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"2","0":"2","activity_name":"\u5934\u8111\u98ce\u66b4","1":"\u5934\u8111\u98ce\u66b4","activity_begin_time":"2020-06-12 21:01:04","2":"2020-06-12 21:01:04","activity_end_time":"2020-06-13 21:01:05","3":"2020-06-13 21:01:05","activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"3","0":"3","activity_name":"\u7455\u79ef\u5206","1":"\u7455\u79ef\u5206","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"4","0":"4","activity_name":"\u8bd7\u6b4c\u6717\u8bf5","1":"\u8bd7\u6b4c\u6717\u8bf5","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"6","0":"6","activity_name":"\u6700\u70ab\u6c11\u65cf\u98ce","1":"\u6700\u70ab\u6c11\u65cf\u98ce","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":"\u5317\u6781","4":"\u5317\u6781","activity_content":"\u5c2c\u821e","5":"\u5c2c\u821e"}]

But a closer look not only outputs the default key-value pairs, but also the default number subscript-value pairs. Emm must remove one of them. Wow, here remove the latter

if ( $query = $conn->query($sql) )
{
   $result = json_encode($query->fetchAll(PDO::FETCH_ASSOC));
   echo $result;
}
[{"activity_no":"1","activity_name":"\u96f7\u96e8","activity_begin_time":"2020-06-12 20:59:56","activity_end_time":"2020-06-15 20:59:58","activity_location":null,"activity_content":null},{"activity_no":"2","activity_name":"\u5934\u8111\u98ce\u66b4","activity_begin_time":"2020-06-12 21:01:04","activity_end_time":"2020-06-13 21:01:05","activity_location":null,"activity_content":null},{"activity_no":"3","activity_name":"\u7455\u79ef\u5206","activity_begin_time":null,"activity_end_time":null,"activity_location":null,"activity_content":null},{"activity_no":"4","activity_name":"\u8bd7\u6b4c\u6717\u8bf5","activity_begin_time":null,"activity_end_time":null,"activity_location":null,"activity_content":null},{"activity_no":"6","activity_name":"\u6700\u70ab\u6c11\u65cf\u98ce","activity_begin_time":null,"activity_end_time":null,"activity_location":"\u5317\u6781","activity_content":"\u5c2c\u821e"}]

reference

  • https://blog.csdn.net/weixin_30469895/article/details/95589933

Intelligent Recommendation

PHP PDO mode data operation

PDO's full name (PHP Data Object) PHP (data application object) can be seen: 1. Represents a connection between PHP and database services; 2. Can be considered as solutions for different issues; 3. It...

PHP PDO $ Statement binding data

When SQL has mysql keywords need to use Package but: The binding field cannot be used...

2019/2/18: php extracts MySQL data and converts it to json format

Write in front My requirement is that the WeChat applet accesses the php of the website release page, and the php logs in to the database to obtain data. But the problem is that the data returned by t...

Convert the data queried by pdo in php into json format (including Chinese garbled solutions)

The data we usually read in the database is generally a two-dimensional associative array First prepare array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES ‘gbk’”) This paragra...

More Recommendation

Python reads four files of TXT, JSON, CSV, XML, and converts the read data into a dictionary, with four data formats

Python reads file transfer dictionary First, refer to article: Second, `python` Read the four files of` txt, json, csv, xml` and convert the read data into a dictionary Third, data 1. `txt` Data: 2. `...

springmvc converts json data

1 Accept JSON format data: 2. Return data in JSON format pom.xml needed by json...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top