PHP - Retrieve MySQLi Data as JSON Format

Submitted by: 
Visitors have accessed this post 407 times.

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

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

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.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.