How to Create XML file from MySQL Database using PHP/MySQLi
Submitted by nurhodelta_17 on Tuesday, March 13, 2018 - 15:35.
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.- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Create an XML file from MySQL Database using PHP/MySQLi</title>
- <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">Create an XML file from MySQL Database</h1>
- <div class="row">
- <div class="col-sm-8 col-sm-offset-2">
- <div class="row">
- <div class="col-sm-2">
- <a href="create_xml.php" class="btn btn-primary btn-sm">Create XML</a>
- </div>
- <div class="col-sm-10">
- <?php
- echo $_SESSION['message'];
- }
- ?>
- </div>
- </div>
- <table class="table table-bordered table-striped" style="margin-top:20px;">
- <thead>
- <th>UserID</th>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- </thead>
- <tbody>
- <?php
- //connection
- $conn = new mysqli('localhost', 'root', '', 'mydatabase');
- $sql = "SELECT * FROM members";
- $query = $conn->query($sql);
- while($row = $query->fetch_array()){
- ?>
- <tr>
- <td><?php echo $row['id']; ?></td>
- <td><?php echo $row['firstname']; ?></td>
- <td><?php echo $row['lastname']; ?></td>
- <td><?php echo $row['address']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </body>
- </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.- <?php
- //connection
- $conn = new mysqli('localhost', 'root', '', 'mydatabase');
- //fetching data from mysql into array
- $sql = "SELECT * FROM members";
- $query = $conn->query($sql);
- while($row = $query->fetch_assoc()){
- $data[] = $row;
- }
- //converting our array into xml file
- //create the xml document
- $xml = new DOMDocument();
- $root = $xml->createElement('users');
- foreach($data as $user){
- $userRow = $root->appendChild($xml->createElement('user'));
- //populate user info
- foreach($user as $key => $val){
- $userRow->appendChild($xml->createElement($key, $val));
- }
- }
- $xml->appendChild($root);
- //make the output pretty
- $xml->formatOutput = true;
- //save xml file
- $xml->save('files/members.xml');
- $_SESSION['message'] = 'XML file created. Check your files folder';
- ?>
Add new comment
- 2034 views