How to Convert MySQL Data Into JSON Format in PHP using PDO

In this tutorial, we are going to learn how to Convert MySQL Data Into JSON Format in PHP using PDO. This is a simple, short source codes, and useful tutorial that we have today. JSON is exchanging format to web development or in the mobile applications. It is easy to convert the data into plain text format if we are going to use it, so let's begin to convert into JSON format. This is our MySQL data which used to an example in this tutorial, so kindly copy and paste this following data into your PHPMyAdmin and it will create a table after you run it.
  1. --
  2. -- Table structure for table `tbl_users`
  3. --
  4.  
  5. CREATE TABLE `tbl_users` (
  6. `id` INT(11) NOT NULL,
  7. `first_name` VARCHAR(25) NOT NULL,
  8. `last_name` VARCHAR(25) NOT NULL
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  10.  
  11. --
  12. -- Dumping data for table `tbl_users`
  13. --
  14.  
  15. INSERT INTO `tbl_users` (`id`, `first_name`, `last_name`) VALUES
  16. (1, 'Captain', 'America'),
  17. (2, 'Super', 'Woman'),
  18. (3, 'Bat', 'Man'),
  19. (4, 'Wolv', 'Verine'),
  20. (5, 'Spider', 'Man'),
  21. (6, 'Wonder', 'Woman');
After that, create a database connection and we are going to use the PDO extension, and this is the code.
  1. <?php
  2. $database_hostname = "localhost";
  3. $database_user = "root";
  4. $database_password = "";
  5. $database_name = "json_tutorial";
  6.  
  7. try{
  8. $database_connection = new PDO("mysql:host=$database_hostname;dbname=$database_name",$database_user,$database_password);
  9. $database_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.  
  11. }catch(PDOException $z){
  12.  
  13. die($z->getMessage());
  14. }
  15. ?>

Display Records using SELECT Statement from MySQL


We are going to fetch our records from the user's table to get the list of users or to display the data.
  1. require_once 'dbconfig.php';
  2.  
  3. $query = "SELECT * FROM tbl_users";
  4. $statement = $database_connection->prepare($query);
  5. $statement->execute();

Convert MySQL Data to an Array


After constructing in the codes above using SELECT Statement, we have to convert the MySQL Data to an Array. Here's the source code below.
  1. $userData_List = array();
  2.  
  3. while($row=$statement->fetch(PDO::FETCH_ASSOC)){
  4. $userData_List['List of Users'][] = $row;
  5. }
  6. echo json_encode($userData_List);

Convert Array to JSON


In this case, we are going to convert Array to JSON using this function and it's called "json_encode();". Kindly add an array name to the open and closed parenthesis, and it shows like this.
  1. echo json_encode($userData_List);

Complete Source Code for Converting MySQL Data Into JSON Format


Here's the full source code in this tutorial to convert the MySQL Data to JSON Format in PHP using PDO Extension.
  1. <?php
  2. require_once 'dbconfig.php';
  3.  
  4. $query = "SELECT * FROM tbl_users";
  5. $statement = $database_connection->prepare($query);
  6. $statement->execute();
  7.  
  8. $userData_List = array();
  9.  
  10. while($row=$statement->fetch(PDO::FETCH_ASSOC)){
  11. $userData_List['List of Users'][] = $row;
  12. }
  13. echo json_encode($userData_List);
  14. ?>
Hope that this simple tutorial that I created may help you to your future projects. Also, for the beginners who want to learn basic of coding in PHP/MySQL and JSON. Enjoy coding. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment