Search for:
Jump to: 
Your Ad Here
Post new topic Reply to topic
Author Message
 Post subject: Exporting Entire Database into an XML file
PostPosted: Wed Sep 16, 2009 1:13 pm 
Offline

Joined: Mon May 12, 2008 11:43 am
Posts: 307
Hi use the below php code for exporting entire database into an XML file
<?php
//connect
$host='localhost';
$user='root';
$pass='root';
$dbname='test';
$link = mysql_connect($host,$user,$pass);
mysql_select_db($dbname,$link);
//get all the tables
$query = 'SHOW TABLES FROM '.$dbname;
$result = mysql_query($query,$link) or die('cannot show tables');
if(mysql_num_rows($result))
{
//prep output
$tab = "\t";
$br = "\n";
$xml = '<?xml version="1.0" encoding="UTF-8"?>'.$br;
$xml.= '<database name="'.$name.'">'.$br;

//for every table...
while($table = mysql_fetch_row($result))
{
//prep table out
$xml.= $tab.'<table name="'.$table[0].'">'.$br;

//get the rows
$query3 = 'SELECT * FROM '.$table[0];
$records = mysql_query($query3,$link) or die('cannot select from table: '.$table[0]);

//table attributes
$attributes = array('name','blob','maxlength','multiple_key','not_null','numeric','primary_key','table','type','default','unique_key','unsigned','zerofill');
$xml.= $tab.$tab.'<columns>'.$br;
$x = 0;
while($x < mysql_num_fields($records))
{
$meta = mysql_fetch_field($records,$x);
$xml.= $tab.$tab.$tab.'<column ';
foreach($attributes as $attribute)
{
$xml.= $attribute.'="'.$meta->$attribute.'" ';
}
$xml.= '/>'.$br;
$x++;
}
$xml.= $tab.$tab.'</columns>'.$br;

//stick the records
$xml.= $tab.$tab.'<records>'.$br;
while($record = mysql_fetch_assoc($records))
{
$xml.= $tab.$tab.$tab.'<record>'.$br;
foreach($record as $key=>$value)
{
$xml.= $tab.$tab.$tab.$tab.'<'.$key.'>'.htmlspecialchars(stripslashes($value)).'</'.$key.'>'.$br;
}
$xml.= $tab.$tab.$tab.'</record>'.$br;
}
$xml.= $tab.$tab.'</records>'.$br;
$xml.= $tab.'</table>'.$br;
}
$xml.= '</database>';

//save file
$handle = fopen($name.'-backup-'.time().'.xml','w+');
fwrite($handle,$xml);
fclose($handle);
}
mysql_close($link);
?>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic    [ 1 post ] 

1, 2, 3, 4, 5 ... 413
Recent topics  Replies   Views   Last post 
No new posts Ecommerce website development and website Design

by iweballey » Fri May 17, 2013 7:15 am in PHP-Code Help

0

146

Fri May 17, 2013 7:15 am

iweballey View the latest post

No new posts How to upgrade and migrate Joomal site

by mallikharjuna rao » Mon Jan 21, 2013 1:40 pm in Joomla

1

344

Thu May 16, 2013 9:53 am

sunitha12 View the latest post

No new posts Benefits of PHP

by hirephpexpert » Fri Sep 07, 2012 4:41 am in CakePHP

6

1574

Wed May 15, 2013 6:25 am

PrettyPrincesG View the latest post

No new posts How to stop user from getting registered in Joomla

by mallikharjuna rao » Tue Jan 22, 2013 5:16 am in Joomla

3

1483

Wed May 15, 2013 6:17 am

PrettyPrincesG View the latest post

No new posts How to write a query in social engine

by fathima_sony » Tue Sep 06, 2011 12:34 pm in Others

2

1756

Tue May 14, 2013 12:09 pm

sanath123 View the latest post


Your Ad Here
cron