How to Convert MySQL Rows into JSON Format in PHP

0 78

Here is the sample MySQL Dump data which are used in this tutorial, run the following sql query in your PHPMyAdmin it will create table with several records.

ok, now make a connection to mysql database using PDO extension here is the code

Select Records From MySQL

to convert mysql records first of all fetch all records from the users table .

Convert MySQL Rows To Array

Here is the code to convert mysql results to an array

Convert Array To JSON String

In PHP there’s a function called json_encode();. is used to convert array to JSON string. just add array name created above and put in this function like this.

Here We Got JSON Sting

this is the final JSON String we have converted MySQL to Array – Array to JSON, contains root element Allusers

Final Code

here is the final code of above all small pieces

Hope you like it, actually recently i had a work on JSON Apis, so i have decided share a small piece of json code. and guys please keep visiting and do share it with your dev friends
that’s it …

تعليقات فيس بوك

اضافة تعليق

قد يعجبك ايضا أكثر من مؤلف