Skip to main content

PHP - Simple Petty Print MySQLi Data

In this tutorial we will create a Simple Petty Print MySQLi Data using MySQLi. This code will petty print your MySQLi data table when the user click the button The code itself self use a PHP mysqli_fetch_assoc() to fetch the data in associative format in a while loop to extract the MySQLi data in order to display as a readable array using print_r(). This is a user-friendly kind of program feel free user it in your program.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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 bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_print, 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_print");
  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. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="" class="navbar-brand">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 - Simple Petty Print MySQLi Data</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  18. <div class="col-md-6">
  19. <form method="POST" action="">
  20. <button class="btn btn-primary" name="print">Petty Print</button>
  21. </form>
  22. <br />
  23. <table class="table table-bordered">
  24. <thead class="alert-info">
  25. <tr>
  26. <th>Firstname</th>
  27. <th>Lastname</th>
  28. <th>Address</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. <?php
  33. require 'conn.php';
  34. $query = mysqli_query($conn, "SELECT * FROM `member` ORDER BY `lastname` ASC") or die(mysqli_error());
  35. while($fetch=mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['address']?></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. <?php
  49. include'print.php';
  50. ?>
  51. </div>
  52. </body>
  53. </html>

Creating the Main Function

This code contains the main function of the application. This code will petty print a MySQLi table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as print.php.

  1. <div class="col-md-6">
  2. <?php
  3. if(ISSET($_POST['print'])){
  4. $query = mysqli_query($conn, "SELECT * FROM `member` ORDER BY `lastname` ASC") or die(mysqli_error());
  5. while($fetch=mysqli_fetch_assoc($query)){
  6. echo"<pre>";
  7. print_r($fetch);
  8. echo"</pre>";
  9. }
  11. }
  12. ?>
  13. </div>

There you have it we successfully created Simple Petty Print MySQLi Data using MySQLi. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.