How to Export MySQL Data Into JSON Format in PHP

Today in this tutorial I will explain How to Export MySQL Data Into JSON Format in PHP.

What is 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 “,”.

how-to-export-mysql-data-into-json-format-in-php-phpcodify

For example:

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:

 

What is 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:

Student table-phpcodify

With SQL query

Records in MySQL

Records in MySQL-phpcodify

Dumped data With Query:

index.php:

Output in JSON format:

Output in JSON format-phpcodify

In this tutorial, we’ve trying to show the basic example of How to Export MySQL Data Into JSON Format in PHP. We hope you have learned how to export MySQL data in JSON using PHP. If you have any questions about export MySQL data, let us know by commenting here.

Ehtesham Mehmood

Ehtesham Mehmood is a Software Engineer from Pakistan.He's deeply involved in Web Development and Internet Marketing, He loves to code in PHP, jQuery, HTML5, CSS3, JavaScript, Ajax,CodeIgniter,Laravel and some other tools. He is a passionate web developer who creates web applications and websites from absolutely scratch.