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

February 6, 2021
PHP
How to Create XML file from MySQL Database using PHP

Tutorial: Create XML file from MySQL Database using PHP/MySQLi with Source Code

About How to Create XML file from MySQL Database using PHP MySQL

This is a tutorial on How to Create XML file from MySQL Database using PHP/MySQLi. I’ve used bootstrap to improve the design of the 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 a Database

Next, we create our MySQL database where we fetch data into an 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 database.

How to Create XML file from MySQL Database using PHP

How to Create XML file from MySQL Database using PHP

Creating a 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
                        session_start();
 
                        if(isset($_SESSION['message'])){
                            echo $_SESSION['message'];
 
                            unset($_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 a Create XML Script

Lastly, we create our script that creates an XML file from our MySQL Database.

Create a new file named create_xml.php and paste the below codes.

<?php
    session_start();
    //connection
    $conn = new mysqli('localhost', 'root', '', 'mydatabase');
 
    //fetching data from mysql into array
    $data = 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);
    header("Content-Type: text/plain");
    //make the output pretty
    $xml->formatOutput = true;
    //save xml file
    $xml->save('files/members.xml');
 
    $_SESSION['message'] = 'XML file created. Check your files folder';
    header('location: index.php');
 
?>

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!


Related Tutorials: Upload XML File to MySQLi in PHP, Import XML File in PHP, Insert New Entry in XML File using PHP, How to Display XML File in PHP

Download Here

 

Leave a Reply

Your email address will not be published. Required fields are marked *