Skip to main content

PHP - Export MySQLi Database To Excel

Screenshot
Body
In this tutorial we will create a Export MySQLi Database To Excel 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 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 bootstrap that has been used for the layout https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_excel, 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(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_excel');
  3.  
  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 you 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 - Export MySQLi Database To Excel</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Member</button>
  18. <form method="POST" action="excel.php">
  19. <button class="btn btn-success pull-right" name="export"><span class="glyphicon glyphicon-print"></span> Export Excel</button>
  20. </form>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Address</th>
  28. <th>Job</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. <?php
  33. require 'conn.php';
  34.  
  35. $query = $conn->query("SELECT * FROM `member`");
  36. while($fetch = $query->fetch_array()){
  37. ?>
  38. <tr>
  39. <td><?php echo $fetch['firstname']?></td>
  40. <td><?php echo $fetch['lastname']?></td>
  41. <td><?php echo $fetch['address']?></td>
  42. <td><?php echo $fetch['job']?></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. </tbody>
  48. </table>
  49. </div>
  50. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  51. <div class="modal-dialog" role="document">
  52. <form action="save_query.php" method="POST">
  53. <div class="modal-content">
  54. <div class="modal-body">
  55. <div class="col-md-2"></div>
  56. <div class="col-md-8">
  57. <div class="form-group">
  58. <label>Firstname</label>
  59. <input type="text" class="form-control" name="firstname"/>
  60. </div>
  61. <div class="form-group">
  62. <label>Lastname</label>
  63. <input type="text" class="form-control" name="lastname"/>
  64. </div>
  65. <div class="form-group">
  66. <label>Address</label>
  67. <input type="text" class="form-control" name="address"/>
  68. </div>
  69. <div class="form-group">
  70. <label>Job</label>
  71. <input type="text" class="form-control" name="job"/>
  72. </div>
  73. </div>
  74. </div>
  75. <div style="clear:both;"></div>
  76. <div class="modal-footer">
  77. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  78. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  79. </div>
  80. </div>
  81. </form>
  82. </div>
  83. </div>
  84. </body>
  85. <script src="js/jquery-3.2.1.min.js"></script>
  86. <script src="js/bootstrap.js"></script>
  87. </html>

Creating PHP Query

This code contains the query of the application. This code will send the input data to the database server when the button is clicked. To do that copy and write these block of codes inside your text editor and save it as save_query.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. if(!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['address']) && !empty($_POST['job'])){
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $address = $_POST['address'];
  9. $job = $_POST['job'];
  10.  
  11. $conn->query("INSERT INTO `member` VALUE('', '$firstname', '$lastname', '$address', '$job')") or die(mysqli_errno());
  12. header('location: index.php');
  13.  
  14. }else{
  15. echo "<script>alert('Please complete the required field!')</script>";
  16. echo "<script>window.location = 'index.php'</script>";
  17. }
  18. }
  19. ?>

Creating Main Function

This code contains the main function of the application. This code will export a xls file when the button is clicked. To do that just kindly copy and write this code inside the text editor, then save it as excel.php
  1. <?php
  2. header("Content-Type: application/xls");
  3. header("Content-Disposition: attachment; filename=download.xls");
  4. header("Pragma: no-cache");
  5. header("Expires: 0");
  6.  
  7. require_once 'conn.php';
  8.  
  9. $output = "";
  10.  
  11. if(ISSET($_POST['export'])){
  12. $output .="
  13. <table>
  14. <thead>
  15. <tr>
  16. <th>ID</th>
  17. <th>Firstname</th>
  18. <th>Lastname</th>
  19. <th>Address</th>
  20. <th>Job</th>
  21. </tr>
  22. <tbody>
  23. ";
  24.  
  25. $query = $conn->query("SELECT * FROM `member`") or die(mysqli_errno());
  26. while($fetch = $query->fetch_array()){
  27.  
  28. $output .= "
  29. <tr>
  30. <td>".$fetch['mem_id']."</td>
  31. <td>".$fetch['firstname']."</td>
  32. <td>".$fetch['lastname']."</td>
  33. <td>".$fetch['address']."</td>
  34. <td>".$fetch['job']."</td>
  35. </tr>
  36. ";
  37. }
  38.  
  39. $output .="
  40. </tbody>
  41.  
  42. </table>
  43. ";
  44.  
  45. echo $output;
  46. }
  47.  
  48. ?>
There you have it we successfully created a Export MySQLi Database To Excel using PHP. 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!!!

Comments

Submitted byram k (not verified)on Thu, 07/15/2021 - 21:29

nice blogs but if you want more clear view visit this URL https://www.max2colors.com/blog/267/export-mysqli-to-excel-in-php-with-code

Add new comment