This is a guest post from Daniele

After a lot of work, I released the new version (2.0) of the MySQLDump class, already introduced by this article.
MySQLDump is a class that allows to do a complete MySQL database backup with php pages.

After the inandrea’s good work there wasn’t too much space for improvements, but I think that these addition can be usefull: now it’s possible to export structures and/or data not only for the selected database but also for a single table.

The following example will explain how to export the structure and the data for the table mytable in the db mydb (the interface to the class is slightly changed from the previous version).

//Include the library
@include_once('lib_dump.php');
//Db connection
$connection = @mysql_connect('127.0.0.1','username','password');
//Create the MySQLDump class instance
//1° parameter: db name
//2° parameter: the exported file that will contain the dump
//3° parameter: create zipped file (true = zipped, false = normal)
//4° parameter: data encode (true = hexadecimal, false = plain text)
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
//Structure export of the table 'mytable'
$dumper->getTableStructure('mytable');
//Data export of the table 'mytable'
$dumper->getTableData('mytable');


In this way we will obtain the complete dump of the tumble mytable in the file dumpfile.sql (with the term dump we refers to a sql instruction that can recreate the table and the data). Of course, if for some reasons we don’t need both structure and data, we can always create the dump only for the table structure or for the data.
Instead, if we want to do the complete backup of the database, we must use these instructions:

@include_once('lib_dump.php');
$connection = @mysql_connect('127.0.0.1','username','password');
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
$dumper->doDump();

Or, to obtain the structure and the data separately:

@include_once('lib_dump.php');
$connection = @mysql_connect('127.0.0.1','username','password');
$dumper = new MySQLDump('mydb','dumpfile.sql',false,false);
$dumper->getDatabaseStructure();
$dumper->getDatabaseData();

I hope that this class can be usefull to you, and I invite you to visit the specific section of my site (http://www.creativefactory.it/lab/) where you can find the complete documentation generated with phpdoc.

Download MySQLDump. Downloads: 3441


Se sei interessato a questo post, potresti anche provare a leggere:

  • No related posts