Skip to main content

How to Create XML file from MySQL Database using PHP/MySQLi

Screenshot
Body

Getting Started

I've used bootstrap to improve the design of presentation of this tutorial. This bootstrap is included in the downloadable of this tutorial but, if you want, you may download bootstrap using this link.

Creating our Database

Next, we create our MySQL database where we fetch data into XML file. I've included a .sql file in the downloadable of this tutorial which is a mysql database file. All you have to do is import the said file. If you have no idea on how to do this, please refer to my tutorial, How import .sql file to restore MySQL database. You should be able to create a database with tables named mydatabase.

Creating our Link to Create XML

Next, we create the link to create our XML by creating a new file and name it as index.php. Also, I've included showing the data in our table in this file.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Create an XML file from MySQL Database using PHP/MySQLi</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Create an XML file from MySQL Database</h1>
  11. <div class="row">
  12. <div class="col-sm-8 col-sm-offset-2">
  13. <div class="row">
  14. <div class="col-sm-2">
  15. <a href="create_xml.php" class="btn btn-primary btn-sm">Create XML</a>
  16. </div>
  17. <div class="col-sm-10">
  18. <?php
  19.  
  20. if(isset($_SESSION['message'])){
  21. echo $_SESSION['message'];
  22.  
  23. unset($_SESSION['message']);
  24. }
  25. ?>
  26. </div>
  27. </div>
  28. <table class="table table-bordered table-striped" style="margin-top:20px;">
  29. <thead>
  30. <th>UserID</th>
  31. <th>Firstname</th>
  32. <th>Lastname</th>
  33. <th>Address</th>
  34. </thead>
  35. <tbody>
  36. <?php
  37. //connection
  38. $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  39.  
  40. $sql = "SELECT * FROM members";
  41. $query = $conn->query($sql);
  42.  
  43. while($row = $query->fetch_array()){
  44. ?>
  45. <tr>
  46. <td><?php echo $row['id']; ?></td>
  47. <td><?php echo $row['firstname']; ?></td>
  48. <td><?php echo $row['lastname']; ?></td>
  49. <td><?php echo $row['address']; ?></td>
  50. </tr>
  51. <?php
  52. }
  53.  
  54. ?>
  55. </tbody>
  56. </table>
  57. </div>
  58. </div>
  59. </div>
  60. </body>
  61. </html>

Creating our Create XML Script

Lastly, we create our script that creates XML file from our MySQL Database. Create a new file named create_xml.php and paste the below codes.
  1. <?php
  2. //connection
  3. $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  4.  
  5. //fetching data from mysql into array
  6. $data = array();
  7. $sql = "SELECT * FROM members";
  8. $query = $conn->query($sql);
  9.  
  10. while($row = $query->fetch_assoc()){
  11. $data[] = $row;
  12. }
  13.  
  14. //converting our array into xml file
  15. //create the xml document
  16. $xml = new DOMDocument();
  17. $root = $xml->createElement('users');
  18.  
  19. foreach($data as $user){
  20. $userRow = $root->appendChild($xml->createElement('user'));
  21.  
  22. //populate user info
  23. foreach($user as $key => $val){
  24. $userRow->appendChild($xml->createElement($key, $val));
  25. }
  26.  
  27. }
  28.  
  29. $xml->appendChild($root);
  30. header("Content-Type: text/plain");
  31. //make the output pretty
  32. $xml->formatOutput = true;
  33. //save xml file
  34. $xml->save('files/members.xml');
  35.  
  36. $_SESSION['message'] = 'XML file created. Check your files folder';
  37. header('location: index.php');
  38.  
  39. ?>
P.S. Don't forget to create a folder named files. That is where the created XML files are saved. That ends this tutorial. Happy Coding :)

Add new comment