• 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 - Export MySQLi Database To Excel

normal2244

Active Directory Auditor
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
165
Likes
176
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.
2018-06-15_14_16_41-localhost_127.0.0.1_db_excel_phpmyadmin_4.7.0.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Can't connect to database"

    )

    ;
  5. }
  6. ?>

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

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member`"

    )

    ;
  35. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  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. <td><?php

    echo

    $fetch

    [

    'job'

    ]

    ?>

    </td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. </tbody>
  47. </table>
  48. </div>
  49. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  50. <div class="modal-dialog" role="document">
  51. <form action="save_query.php" method="POST">
  52. <div class="modal-content">
  53. <div class="modal-body">
  54. <div class="col-md-2"></div>
  55. <div class="col-md-8">
  56. <div class="form-group">
  57. <label>Firstname</label>
  58. <input type="text" class="form-control" name="firstname"/>
  59. </div>
  60. <div class="form-group">
  61. <label>Lastname</label>
  62. <input type="text" class="form-control" name="lastname"/>
  63. </div>
  64. <div class="form-group">
  65. <label>Address</label>
  66. <input type="text" class="form-control" name="address"/>
  67. </div>
  68. <div class="form-group">
  69. <label>Job</label>
  70. <input type="text" class="form-control" name="job"/>
  71. </div>
  72. </div>
  73. </div>
  74. <div style="clear:both;"></div>
  75. <div class="modal-footer">
  76. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  77. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  78. </div>
  79. </div>
  80. </form>
  81. </div>
  82. </div>
  83. </body>
  84. <script src="js/jquery-3.2.1.min.js"></script>
  85. <script src="js/bootstrap.js"></script>
  86. </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. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. if

    (

    !

    empty

    (

    $_POST

    [

    'firstname'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'lastname'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'address'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'job'

    ]

    )

    )

    {
  5. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  6. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  8. $job

    =

    $_POST

    [

    'job'

    ]

    ;

  9. $conn

    ->

    query

    (

    "INSERT INTO `member` VALUE('', '$firstname

    ', '$lastname

    ', '$address

    ', '$job

    ')"

    )

    or die

    (

    mysqli_errno

    (

    )

    )

    ;
  10. header

    (

    'location: index.php'

    )

    ;

  11. }

    else

    {
  12. echo

    "<script>alert('Please complete the required field!')</script>"

    ;
  13. echo

    "<script>window.location = 'index.php'</script>"

    ;
  14. }
  15. }
  16. ?>

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. require_once

    'conn.php'

    ;

  7. $output

    =

    ""

    ;

  8. if

    (

    ISSET

    (

    $_POST

    [

    'export'

    ]

    )

    )

    {
  9. $output

    .=

    "
  10. <table>
  11. <thead>
  12. <tr>
  13. <th>ID</th>
  14. <th>Firstname</th>
  15. <th>Lastname</th>
  16. <th>Address</th>
  17. <th>Job</th>
  18. </tr>
  19. <tbody>
  20. "

    ;

  21. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member`"

    )

    or die

    (

    mysqli_errno

    (

    )

    )

    ;
  22. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {

  23. $output

    .=

    "
  24. <tr>
  25. <td>"

    .

    $fetch

    [

    'mem_id'

    ]

    .

    "</td>
  26. <td>"

    .

    $fetch

    [

    'firstname'

    ]

    .

    "</td>
  27. <td>"

    .

    $fetch

    [

    'lastname'

    ]

    .

    "</td>
  28. <td>"

    .

    $fetch

    [

    'address'

    ]

    .

    "</td>
  29. <td>"

    .

    $fetch

    [

    'job'

    ]

    .

    "</td>
  30. </tr>
  31. "

    ;
  32. }

  33. $output

    .=

    "
  34. </tbody>

  35. </table>
  36. "

    ;

  37. echo

    $output

    ;
  38. }

  39. ?>

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


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,500

350,639

350,649

Top