Dynamically Create XML file from MySQL Database using PHP MySQLi

Dynamically Create XML file from MySQL Database using PHP MySQLi

In this Post We Will Explain About is Dynamically Create XML file from MySQL Database using PHP MySQLi With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Create XML file using PHP and MySQLi Example

In this post we will show you Best way to implement How to generate xml files using php and MySQLi, hear for How to Create XML file from MySQL Database using PHP MySQLi with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :   AngularJS CRUD Insert Update Delete with PHP and MySQL




making simple Link to Create XML

Next, we make the link to make simple PHP XML by making a new root main file and name Like as index.php. As well as, We have included dislaing the data in simple mysql table in this file.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>how to fetch data from database in xml format using php and mysqli</title>
	<link rel="stylesheet" type="text/css" href="live/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="live24u container">
	<h1 class="live24u page-header text-center">How to generate xml files using php and MySQLi</h1>
	<div class="live24u row">
		<div class="live24u col-sm-8 col-sm-offset-2">
			<div class="live24u row">
				<div class="live24u col-sm-2">
					<a href="make_php_xml.php" class="btn btn-success btn-sm">Create XML</a>
				</div>
				<div class="col-sm-10">
					<?php
						session_start();
 
						if(isset($_SESSION['comments'])){
							echo $_SESSION['comments'];
 
							unset($_SESSION['comments']);
						}
					?>
				</div>
			</div>
			<table class="table table-bordered table-striped" style="margin-top:20px;">
				<thead>
					<th>StudentId</th>
					<th>StudentFname</th>
					<th>StudentLname</th>
					<th>StudentAddress</th>
				</thead>
				<tbody>
					<?php
					//Database connection
					$conn = new mysqli('localhost', 'username', '', 'live24u');
 
					$lstsql = "SELECT * FROM students";
					$myquery = $conn->query($lstsql);
 
					while($dbrow = $myquery->fetch_array()){
						?>
						<tr>
							<td><?php echo $dbrow['id']; ?></td>
							<td><?php echo $dbrow['firstname']; ?></td>
							<td><?php echo $dbrow['lastname']; ?></td>
							<td><?php echo $dbrow['address']; ?></td>
						</tr>
						<?php
					}
 
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
</body>
</html>

Creating simple Create XML Script

Last Phase, we Make simple source script that makes or generated XML file from simple PHP and MySQL Database.

Also Read :   Get Days Difference Between Two Dates using laravel

Make a new file named Like as make_php_xml.php as well as simple copy and paste the below source codes.

<?php
	session_start();
	//Database connection
	$conn = new mysqli('localhost', 'your_user_name', '', 'live24u');
 
	//Retriving studdata from PHP mysql into multiple array
	$studdata = array();
	$sql = "SELECT * FROM students";
	$query = $conn->query($sql);
 
	while($row = $query->fetch_assoc()){
		$studdata[] = $row;
	}
 
	//studdata converting simple array into livexml file
    //create the xml document
    $livexml = new DOMDocument();
    $root = $livexml->createElement('students');
 
    foreach($studdata as $stud){
            $userRow = $root->appendChild($livexml->createElement('stud'));
 
            //populate stud info
            foreach($stud as $key => $val){
                $userRow->appendChild($livexml->createElement($key, $val));
            }
 
    }
 
    $livexml->appendChild($root);
    header("Content-Type: text/plain");
    //created the output good Luck
    $livexml->formatOutput = true;
    //store or save livexml file
    $livexml->save('files/students.xml');
 
    $_SESSION['comments'] = 'Good Luck, Your XML file created.Now Check your files all the folder';
    header('location: index.php');
 
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :   Calculate the difference between two dates using PHP





Example

I hope you have Got What is Create XML file using PHP and MySQLi And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.