• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

PHP - Convert MYSQLi Data To JSON

MaxGamesAlts

Humor Alchemist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
136
Likes
41
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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.
2018-06-29_13_40_21-localhost_127.0.0.1_db_product_phpmyadmin_4.7.0.png

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. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    $database

    )

    ;

  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Error: Can't 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 - 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. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $product_name

    =

    $_POST

    [

    'product_name'

    ]

    ;
  5. $product_price

    =

    $_POST

    [

    'product_price'

    ]

    ;

  6. $conn

    ->

    query

    (

    "INSERT INTO `product` VALUES('', '$product_name

    ', '$product_price

    ')"

    )

    ;

  7. header

    (

    'location: index.php'

    )

    ;
  8. }
  9. ?>

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. $output

    =

    ""

    ;

  4. header

    (

    "Content-Type: application/txt"

    )

    ;
  5. header

    (

    "Content-Disposition: attachment; filename="

    .

    $database

    .

    ".txt"

    )

    ;
  6. header

    (

    "Pragma: no-cache"

    )

    ;
  7. header

    (

    "Expires: 0"

    )

    ;

  8. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `product`"

    )

    ;

  9. $array

    =

    array

    (

    )

    ;

  10. while

    (

    $fetch

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  11. $array

    [

    ]

    =

    $fetch

    ;
  12. }

  13. $output

    .=
  14. "<pre>"

    .

    print_r

    (

    $array

    )

    .

    '</pre>'

    ;

  15. ?>

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!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,821

349,831

Top