How to Take Backup of Database into XML with PHP

images (6)

backup is an important part of complex web design. Most of the popular websites allow users to download the copy of their daya in XML format. Database backup in XML format is the popular way of taking backup. In Blogger and WordPress, you can see the example which use XML format for taking backup of blog posts. You can also create the same thing for your website or CMS. You can easily backup database into XML format. See the PHP code snippet. The script below reads the schema from a MySQL database and outputs XML that describes the schema.

See the code example of Database backup in XML with PHP


$dbhandle = mysql_connect('localhost', 'root', 'password')
 or die("Unable to connect to MySQL");

$selected = mysql_select_db('webtips', $dbhandle)
 or die("Could not select examples");

$result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle );

$tables = array();
while ($row = mysql_fetch_row($result_tbl)) {
 $tables[] = $row[0];

$output = "<?xml version=\"1.0\" ?>\n";
$output .= "<schema>";

// iterate over each table and return the fields for each table
foreach ( $tables as $table ) {
 $output .= "<table name=\"$table\">";
 $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

while( $row1 = mysql_fetch_row($result_fld) ) {
 $output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
 $output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />" : " />";

$output .= "</table>";

$output .= "</schema>";

header("Content-type: text/xml");
echo $output;

Tags: | |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.

Similar Articles


Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker