Export MySQL Data Into JSON Format Using PHP
Hello guys. Today I will explain how to create or export data from a database format in JavaScript Object Notation (JSON) using PHP MySQL.
JSON
JSON is a lightweight format for data exchange. It is easy for humans to read and write. It is easy for machines to parse and generate. It is lightweight, easy to read and easy to use for exchanging data across multiple platforms.
Today most APIs social networking applications like Facebook and Twitter using JSON data interchange format.
A JSON array begins with "[" and ends with "]". Among them, a series of values can reside. If more than one value below which are separated by "."
For instance:
JSON
JSON is a lightweight format for data exchange. It is easy for humans to read and write. It is easy for machines to parse and generate. It is lightweight, easy to read and easy to use for exchanging data across multiple platforms.
Today most APIs social networking applications like Facebook and Twitter using JSON data interchange format.
A JSON array begins with "[" and ends with "]". Among them, a series of values can reside. If more than one value below which are separated by "."
For instance:
- [
- {"id":"1","name":"Ehtesham","roll_no":"131","degree":"BSCS"},
- {"id":"2","name":"Raza","roll_no":"135","degree":"BSCS"}
- ]
JSON Object
An object begins with "{" dy ends with "}". Among them, a series of name / value pairs can reside chain. The name and value is separated by a ":" and if there is more than one name / value pairs then they are separated by ",".
For example:
{"id":"1","name":"Ehtesham","roll_no":"131","degree":"BSCS"}
PDO
The PHP Data Objects (PDO) defines a lightweight and consistent access to databases on the PHP interface. PDO provides access to data abstraction layer, which means that regardless of which database you are using, the same functions are used to query and retrieve data. Simply change the database drivers. Let's start with him. PHP reference.
Student table:
With SQL query
- CREATE TABLE IF NOT EXISTS `student` (
- `id` int(10) NOT NULL AUTO_INCREMENT,
- `name` varchar(255) NOT NULL,
- `roll_no` varchar(255) NOT NULL,
- `degree` text NOT NULL,
- PRIMARY KEY (`id`)
- )
Records in MySQL
With Query:
- INSERT INTO `student` (`id`, `name`, `roll_no`, `degree`) VALUES
- (1, 'Ehtesham', '131', 'BSCS'),
- (2, 'Raza', '135', 'BSCS'),
- (3, 'Zaryab', '117', 'BSCS'),
- (4, 'Zaid', '112', 'BSCS'),
- (5, 'Farrukh', '1244', 'BS Telecommunication'),
- (6, 'Salman', '084', 'BSCS');
index.php:
- <?php
- //PDO is a extension which defines a lightweight, consistent interface for accessing databases in PHP.
- $db=new PDO('mysql:dbname=jason;host=localhost;','root','');
- //here prepare the query for analyzing, prepared statements use less resources and thus run faster
- $row=$db->prepare('select * from student');
- $row->execute();//execute the query
- $json_data=array();//create the array
- foreach($row as $rec)//foreach loop
- {
- $json_array['id']=$rec['id'];
- $json_array['name']=$rec['name'];
- $json_array['roll_no']=$rec['roll_no'];
- $json_array['degree']=$rec['degree'];
- //here pushing the values in to an array
- array_push($json_data,$json_array);
- }
- //built in PHP function to encode the data in to JSON format
- echo json_encode($json_data);
- ?>
Output in JSON format:
Export MySQL Data Into JSON Format Using PHP
Reviewed by Unknown
on
03:31
Rating:
No comments: