Hi guys. Today I will explain how to create or export data from a MySQL database into JavaScript Object Notation (JSON) format using PHP. 
Note:Now you can read it on my blog.I have updated this article on my personal blog here.
JSON
JSON is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. it's lightweight, readable and easily manageable for exchanging data across various platforms.
Now a days most of the social networking application APIs like Facebook and Twitter use JSON as data exchange format.
A JSON Array starts with "[" and ends with "]". Between them, a number of values can reside. If there are more than one value then they are separated by ",".
For example:
- [
- {"id":"1","name":"Ehtesham","roll_no":"131","degree":"BSCS"},
- {"id":"2","name":"Raza","roll_no":"135","degree":"BSCS"}
- ]
JSON Object
An object starts with "{" and ends with "}". Between them, a number of string name/value pairs can reside. 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) extension defines a lightweight, consistent interface for accessing databases in PHP. PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. You just need to change the database drivers. Let's start with it. Reference by PHP.
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`)
- )



sanghamitra mishraPosted Jun 22, 2018, 4:53 AM
How to get the data store procedure using php in JSON format?
Teet EPosted May 25, 2018, 5:05 AM
How can you generate JSON , without hardcoding the SQL column keys into it ?
Alex CimpeanuPosted May 17, 2018, 9:50 AM
Ehtesham can i sort the result by a value?
Aquila MediaPosted Feb 7, 2018, 8:06 AM
Hello, You can help me. How to i can creat a json this same: { "1":{ "url":"http:\/\/demo.com\/", "name":"Demo.com", "timer":"5", "avg":"5", "h":"60" }, "2":{ "url":"http:\/\/demo.com\/", "name":"Demo.com", "timer":"5", "avg":"5", "h":"60" } }
Prasanna MuraliPosted Sep 5, 2016, 11:02 AM
Nice post......
jeetesh pandeyPosted Dec 15, 2015, 6:12 AM
how to get value json array from ajax php
George ObonyaPosted May 24, 2015, 1:07 PM
Hi Guys is there a way how to remove the quotes from every number value and save it to the disk as file_name.json in PHP ?? Thanks guys.
Frank GutierrezPosted May 24, 2015, 12:27 AM
well explained
Ehtesham MehmoodPosted Apr 13, 2015, 12:38 PM
Thank you so much :) Vimal Saifudin
Vimal SaifudinPosted Apr 10, 2015, 1:59 PM
Good One Ehtesham Mehmood .. It helped me.. Keep up Great work !!!!!!!