Generate XML File Using PHP And MySQL |
X

Congrats, You are Subscribed to Receive Updates.

Generate XML File Using PHP And MySQL


Introduction

XML files are the best way to handle the export and imports or data’s. Generate XML file using PHP and MySQL is not much complicated. Also which is good to handle backups and external site duplicate with the contents and settings.  Mainly you can keep the demo data’s and settings to keep your theme or plugin demo can show the full features with the XML imports and Exports.

Mysql tables can be fetched into the XML files. Let me make you the sample of XML File Creation and make the copy from database to XML. Let’s try the code with sample MySQL Database.

CREATE TABLE IF NOT EXISTS `users` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(255) NOT NULL DEFAULT '',
 `password` varchar(60) NOT NULL DEFAULT '',
 `full_name` varchar(250) NOT NULL,
 `activation` varchar(30) DEFAULT NULL,
 `email` varchar(255) NOT NULL DEFAULT '',
 `role` varchar(30) NOT NULL,
 `status` varchar(30) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `users` (`ID`, `username`, `password`, `full_name`, `activation`, `email`, `role`, `status`) VALUES
(1, 'root', '$P$JaNAqhV1grd8jTLrqcuFdf1AloMH6m.', 'root', NULL, 'info@yourwebsite.com', 'Administrator', 'Active');

The Above one will be simple query.  And the database connectivity code.

 define('DBHOST','localhost');
 define('DBUSER','root');
 define('DBPASS','123');
 define('DBNAME','neem_cms');
 define('TB_PREF','cms_');

$db = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
if(!$db){
 die( "Sorry! There seems to be a problem connecting to our database.");
}

Generate-XML-File-Using-PHP-And-MySQL

Creating XML File

And Now,  Let’s Create a XML file with write mode.

$myFile = "rss.xml";
$fh = fopen($myFile, 'w') or die("can't open file"); 

Generate XML File Using PHP And MySQL

The Below code helps you to query from users table and create content of XML file .

 $rss_txt .= '<?xml version="1.0" encoding="utf-8"?>';
 $rss_txt .= "<rss version='2.0'>".PHP_EOL;
 $rss_txt .= '<users>'.PHP_EOL;
 $query = mysqli_query($db, "SELECT * FROM ".TB_PREF."users");
 while($values_query = mysqli_fetch_assoc($query)) {
 $rss_txt .= '<user>';
 $rss_txt .= '<ID>' .$values_query['ID']. '</ID>';
 $rss_txt .= '<username>' .$values_query['username']. '</username>';
 $rss_txt .= '<password>' .$values_query['password']. '</password>';
 $rss_txt .= '<full_name>' .$values_query['full_name']. '</full_name>';
 $rss_txt .= '<activation>' .$values_query['activation']. '</activation>';
 $rss_txt .= '<email>' .$values_query['email']. '</email>';
 $rss_txt .= '<role>' .$values_query['role']. '</role>';
 $rss_txt .= '<status>' .$values_query['status']. '</status>';
 $rss_txt .= '</user>'.PHP_EOL;
 }
$rss_txt .= '</users>'.PHP_EOL;
$rss_txt .= '</rss>';
fwrite($fh, $rss_txt);
fclose($fh);

That’s it. You can check this code on your localhost. It will create a new file on the same directory and write your content in it. If its not working,  Please check error log and also the permission of the directory. Hope you like this article, If you are interested to follow me on social sites please use the below links also you can comment on next tab.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

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

*

Current ye@r *

Menu

Sidebar