how to retrieve data from database in json format using php?

how to retrieve data from database in json format using PHP Script for fetching data from mysql and store in JSON file.

how to retrieve data from database in json format using php

To generate json string we will use php function json_encode(). i am going to learn, how to extract data from MySQL database and export it to a file in JSON format, using PHP.

How to Convert Data from MySQL to JSON using PHP?

Create MySQL Database

CREATE TABLE IF NOT EXISTS `tbl_member` (
  `member_id` int(4) NOT NULL AUTO_INCREMENT,
  `member_name` varchar(60) NOT NULL,
  `nominate` varchar(30) NOT NULL,
  `join_dt` date NOT NULL,
  `earning` int(10) NOT NULL,
  PRIMARY KEY (`member_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `tbl_member` (`member_id`, `member_name`, `nominate`, `join_dt`, `earning`) VALUES
(1, 'Dipti', 'tester', '2022-08-01', 60000),
(2, 'Rekha', 'Bolwer', '2029-10-09', 20000),
(3, 'Kavita', 'Batsmen', '2022-08-20', 40000),
(4, 'Sejal', 'Bolwer', '2022-06-01', 25000),
(5, 'Meera', 'Player', '2029-10-01', 10000);

Convert MySQL to JSON String in PHP

Step 1: Open MySQL Database Connection in PHP
using mysqli_connect() function.


Step 2: Fetch Data from MySQL Database


Step 3: Convert MySQL Result Set to PHP Array


Don’t Miss : Display JSON Data From Database Using Php Using Jquery Ajax

Step 4: Convert PHP Array to JSON String



Convert MySQL to JSON File in PHP


I hope you get an idea about how to retrieve data from database in json format using php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment