PHP - Retrieve MySQLi Data as JSON Format

In this tutorial we will create a Retrieve MySQLi Data as JSON Format. By 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. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. So Let's do the coding...

Getting 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_json, 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. $conn = mysqli_connect("localhost", "root", "", "db_json");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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 - Retrieve MySQLi Data as JSON Format</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  18. <br /><br />
  19. <div class="col-md-6">
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <?php
  29. require 'conn.php';
  30.  
  31. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  32. while($fetch = mysqli_fetch_array($query)){
  33. ?>
  34. <tr>
  35. <td><?php echo $fetch['firstname']?></td>
  36. <td><?php echo $fetch['lastname']?></td>
  37. </tr>
  38. <?php
  39. }
  40. ?>
  41. </tbody>
  42. <tfooter>
  43. <tr>
  44. <td colspan="2">
  45. <form method="POST">
  46. <button class="btn btn-primary" name="retrieve">Retrieve JSON</button>
  47. </form>
  48. </td>
  49. </tr>
  50. </tfooter>
  51. </table>
  52. </div>
  53. <div class="col-md-6" style="word-wrap:break-word; border:1px SOLID #ccc;">
  54. <?php include 'retrieve.php'?>
  55. </div>
  56. </div>
  57. <div id="form_modal" class="modal fade" aria-hidden="true">
  58. <div class="modal-dialog">
  59. <div class="modal-content">
  60. <form method="POST" action="save.php">
  61. <div class="modal-header">
  62. <h3 class="modal-title">Add member</h3>
  63. </div>
  64. <div class="modal-body">
  65. <div class="col-md-3"></div>
  66. <div class="col-md-6">
  67. <div class="form-group">
  68. <label>Firstname</label>
  69. <input type="text" name="firstname" class="form-control"/>
  70. </div>
  71. <div class="form-group">
  72. <label>Lastname</label>
  73. <input type="text" name="lastname" class="form-control"/>
  74. </div>
  75. </div>
  76. </div>
  77. <br style="clear:both;"/>
  78. <div class="modal-footer">
  79. <button type="button" data-dismiss="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Close</button>
  80. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  81. </div>
  82. </form>
  83. </div>
  84. </div>
  85. </div>
  86. <script src="js/jquery-3.2.1.min.js"></script>
  87. <script src="js/bootstrap.js"></script>
  88. </body>
  89. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the data inputs to the MySQL database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7.  
  8. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname')") or die(mysqli_error());
  9.  
  10. header("location:index.php");
  11. }
  12. ?>

Creating the Main Function

This code contains the main function of the application. This code will forcefully convert the MySQLi data into JSON array when the button is clicked. To do that just kindly copy and write these block of codes inside the text editor, then save it as retrieve.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['retrieve'])){
  5. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  6.  
  7. $array = array();
  8.  
  9. while($fetch = mysqli_fetch_array($query)){
  10. $array[] = $fetch;
  11. }
  12.  
  13.  
  14. echo json_encode($array);
  15. }
  16. ?>
There you have it we successfully created Retrieve MySQLi Data as JSON Format. 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