PHP - Convert MYSQLi Data To JSON

In this tutorial we will create a Convert MYSQLi Data To JSON using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_product, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $database = "db_product";
  3.        
  4.         $conn = new mysqli('localhost', 'root', '', $database);
  5.        
  6.         if(!$conn){
  7.                 die("Error: Can't Connect to database!");
  8.         }
  9. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Convert MYSQLi Data To JSON</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead>
  21.                                 <tr>
  22.                                         <td>Product ID</td>
  23.                                         <td>Product Name</td>
  24.                                         <td>Price</td>
  25.                                 </tr>
  26.                         </thead>
  27.                         <tbody>
  28.                                 <?php
  29.                                         require 'conn.php';
  30.                                         $query = $conn->query("SELECT * FROM `product`");
  31.                                         while($fetch = $query->fetch_array()){
  32.                                 ?>
  33.                                 <tr>
  34.                                         <td><?php echo $fetch['product_id']?></td>
  35.                                         <td><?php echo $fetch['product_name']?></td>
  36.                                         <td><?php echo $fetch['product_price']?></td>
  37.                                 </tr>
  38.                                 <?php
  39.                                         }
  40.                                 ?>
  41.                         </tbody>
  42.                 </table>
  43.                 <form method="POST" action="export.php">
  44.                         <button class="btn btn-success pull-right"><span class="glyphicon glyphicon-export"></span> Export as JSON</button>
  45.                 </form>
  46.         </div>
  47.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  48.                 <div class="modal-dialog" role="document">
  49.                         <form action="save_query.php" method="POST">
  50.                                 <div class="modal-content">
  51.                                         <div class="modal-body">
  52.                                                 <div class="col-md-2"></div>
  53.                                                 <div class="col-md-8">
  54.                                                         <div class="form-group">
  55.                                                                 <label>Product Name</label>
  56.                                                                 <input class="form-control" type="text" name="product_name">
  57.                                                         </div>
  58.                                                         <div class="form-group">
  59.                                                                 <label>Price</label>
  60.                                                                 <input class="form-control" type="text" name="product_price" />
  61.                                                         </div>
  62.                                                 </div>
  63.                                         </div>
  64.                                         <div style="clear:both;"></div>
  65.                                         <div class="modal-footer">
  66.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  67.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  68.                                         </div>
  69.                                 </div>
  70.                         </form>
  71.                 </div>
  72.         </div>
  73. </body>
  74. <script src="js/jquery-3.2.1.min.js"></script>
  75. <script src="js/bootstrap.js"></script>
  76. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the php server when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as save_query.php.
  1. <?php
  2.         require_once 'conn.php';
  3.  
  4.         if(ISSET($_POST['save'])){
  5.                 $product_name = $_POST['product_name'];
  6.                 $product_price = $_POST['product_price'];
  7.                        
  8.                 $conn->query("INSERT INTO `product` VALUES('', '$product_name', '$product_price')");
  9.                
  10.                 header('location: index.php');
  11.         }
  12. ?>

Creating Convert Script

This is the main function of the application. This code contains the main process of the app when the user click the button. It will extract the data from the database then will be force to create a text file that contains the MySQLi data as a JSON array.
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         $output = "";
  5.        
  6.         header("Content-Type: application/txt");    
  7.         header("Content-Disposition: attachment; filename=".$database.".txt");  
  8.         header("Pragma: no-cache");
  9.         header("Expires: 0");
  10.  
  11.         $query = $conn->query("SELECT * FROM `product`");
  12.        
  13.         $array = array();
  14.        
  15.         while($fetch = $query->fetch_assoc()){
  16.                 $array[] = $fetch;
  17.         }
  18.  
  19.         $output .=
  20.         "<pre>".print_r($array).'</pre>';
  21.        
  22. ?>
There you have it we successfully created Convert MYSQLi Data To JSON. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment