Our blog
PHP MySQL Dump Script
Don't get me wrong, I love phpMyAdmin and use it every day. However sometimes on client servers the only access to the database is via phpMyAdmin and it hasn't been set up properly. One really irritating example is when trying to dump a database so that it can be installed on my local development server. Do the export etc etc and then phpMyAdmin gives you the download, but no - its an empty file...
After messing around for 10 minutes you realise that no matter what you do, you aint getting your DB dump this way.
In steps this script, as mentioned here.
Just in case that link dies - here is the body of the script - with all credit and thanks to the original author:
-
<?php
-
-
-
/*---------------------------------------------------+
-
| mysqldump.php
-
+----------------------------------------------------+
-
| Copyright 2006 Huang Kai
-
| hkai@atutility.com
-
| http://atutility.com/
-
+----------------------------------------------------+
-
| Released under the terms & conditions of v2 of the
-
| GNU General Public License. For details refer to
-
| the included gpl.txt file or visit http://gnu.org
-
+----------------------------------------------------*/
-
-
/*
-
change log:
-
2006-10-16 Huang Kai
-
---------------------------------
-
initial release
-
2006-10-18 Huang Kai
-
---------------------------------
-
fixed bugs with delimiter
-
add paramter header to add field name as CSV file header.
-
2006-11-11 Huang Kia
-
Tested with IE and fixed the <button> to <input>
-
*/
-
-
$mysqldump_version="1.02";
-
-
-
-
$print_form=1;
-
-
-
-
-
-
-
//test mysql connection
-
-
-
{
-
-
$mysql_host=$_REQUEST['mysql_host'];
-
-
$mysql_database=$_REQUEST['mysql_database'];
-
-
$mysql_username=$_REQUEST['mysql_username'];
-
-
$mysql_password=$_REQUEST['mysql_password'];
-
-
-
-
if( 'Test Connection' == $_REQUEST['action'])
-
-
{
-
-
_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
-
-
}
-
-
else if( 'Export' == $_REQUEST['action'])
-
-
{
-
-
_mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
-
-
if( 'SQL' == $_REQUEST['output_format'] )
-
-
{
-
-
$print_form=0;
-
-
-
-
//ob_start("ob_gzhandler");
-
-
-
-
echo "/*mysqldump.php version $mysqldump_version */\n";
-
-
_mysqldump($mysql_database);
-
-
-
-
//header("Content-Length: ".ob_get_length());
-
-
-
-
//ob_end_flush();
-
-
}
-
-
-
{
-
-
$print_form=0;
-
-
-
-
-
-
-
-
-
//header('Content-type: text/plain');
-
-
_mysqldump_csv($_REQUEST['mysql_table']);
-
-
-
-
}
-
-
}
-
-
-
-
}
-
-
-
-
function _mysqldump_csv($table)
-
-
{
-
-
$delimiter= ",";
-
-
-
$delimiter= $_REQUEST['csv_delimiter'];
-
-
-
-
if( 'Tab' == $delimiter)
-
-
$delimiter="\t";
-
-
-
-
-
-
$sql="select * from `$table`;";
-
-
-
if( $result)
-
-
{
-
-
-
-
-
-
$i=0;
-
-
while( $i <$num_fields)
-
-
{
-
-
-
-
if( $i <$num_fields-1)
-
-
echo "$delimiter";
-
-
$i++;
-
-
}
-
-
echo "\n";
-
-
-
-
if( $num_rows> 0)
-
-
{
-
-
-
{
-
-
for( $i=0; $i <$num_fields; $i++)
-
-
{
-
-
-
if( $i <$num_fields-1)
-
-
echo "$delimiter";
-
-
}
-
-
echo "\n";
-
-
}
-
-
-
-
}
-
-
}
-
-
-
-
-
}
-
-
-
-
-
-
function _mysqldump($mysql_database)
-
-
{
-
-
$sql="show tables;";
-
-
-
if( $result)
-
-
{
-
-
-
{
-
-
_mysqldump_table_structure($row[0]);
-
-
-
-
-
{
-
-
_mysqldump_table_data($row[0]);
-
-
}
-
-
}
-
-
}
-
-
else
-
-
{
-
-
echo "/* no tables in $mysql_database */\n";
-
-
}
-
-
-
}
-
-
-
-
function _mysqldump_table_structure($table)
-
-
{
-
-
echo "/* Table structure for table `$table` */\n";
-
-
-
{
-
-
echo "DROP TABLE IF EXISTS `$table`;\n\n";
-
-
}
-
-
-
{
-
-
-
-
$sql="show create table `$table`; ";
-
-
-
if( $result)
-
-
{
-
-
-
{
-
-
-
}
-
-
}
-
-
-
}
-
-
}
-
-
-
-
function _mysqldump_table_data($table)
-
-
{
-
-
-
-
$sql="select * from `$table`;";
-
-
-
if( $result)
-
-
{
-
-
-
-
-
-
if( $num_rows> 0)
-
-
{
-
-
echo "/* dumping data for table `$table` */\n";
-
-
-
-
-
$i=0;
-
-
while( $i <$num_fields)
-
-
{
-
-
-
-
$i++;
-
-
}
-
-
-
-
//print_r( $field_type);
-
-
echo "insert into `$table` values\n";
-
-
$index=0;
-
-
-
{
-
-
echo "(";
-
-
for( $i=0; $i <$num_fields; $i++)
-
-
{
-
-
-
echo "null";
-
-
else
-
-
{
-
-
switch( $field_type[$i])
-
-
{
-
-
case 'int':
-
-
-
break;
-
-
case 'string':
-
-
case 'blob' :
-
-
default:
-
-
-
-
-
}
-
-
}
-
-
if( $i <$num_fields-1)
-
-
echo ",";
-
-
}
-
-
echo ")";
-
-
-
-
if( $index <$num_rows-1)
-
-
echo ",";
-
-
else
-
-
echo ";";
-
-
echo "\n";
-
-
-
-
$index++;
-
-
}
-
-
}
-
-
}
-
-
-
echo "\n";
-
-
}
-
-
-
-
function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
-
-
{
-
-
global $output_messages;
-
-
-
if (!$link)
-
-
{
-
-
-
}
-
-
else
-
-
{
-
-
array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");
-
-
-
-
-
if (!$db_selected)
-
-
{
-
-
-
}
-
-
else
-
-
-
}
-
-
-
-
}
-
-
-
-
if( $print_form>0 )
-
-
{
-
-
?>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
-
-
-
<html>
-
-
<head>
-
-
-
</head>
-
-
-
-
<body>
-
-
<?php
-
-
foreach ($output_messages as $message)
-
-
{
-
-
-
}
-
-
?>
-
-
<form action="" method="post">
-
-
MySQL connection parameters:
-
-
<table border="0">
-
-
<tr>
-
-
<td>Host:</td>
-
-
<td><input name="mysql_host" value="<?php if(isset($_REQUEST['mysql_host']))echo $_REQUEST['mysql_host']; else echo 'localhost';?>" /></td>
-
-
</tr>
-
-
<tr>
-
-
<td>Database:</td>
-
-
<td><input name="mysql_database" value="<?php echo $_REQUEST['mysql_database']; ?>" /></td>
-
-
</tr>
-
-
<tr>
-
-
<td>Username:</td>
-
-
<td><input name="mysql_username" value="<?php echo $_REQUEST['mysql_username']; ?>" /></td>
-
-
</tr>
-
-
<tr>
-
-
<td>Password:</td>
-
-
<td><input type="password" name="mysql_password" value="<?php echo $_REQUEST['mysql_password']; ?>" /></td>
-
-
</tr>
-
-
<tr>
-
-
<td>Output format: </td>
-
-
<td>
-
-
<select name="output_format">
-
-
-
-
-
-
</select>
-
-
</td>
-
-
</tr>
-
-
</table>
-
-
<input type="submit" name="action" value="Test Connection"><br />
-
-
-
-
<br>Dump options(SQL):
-
-
<table border="0">
-
-
-
-
<tr>
-
-
<td>Drop table statement: </td>
-
-
-
</tr>
-
-
<tr>
-
-
<td>Create table statement: </td>
-
-
-
</tr>
-
-
<tr>
-
-
<td>Table data: </td>
-
-
-
</tr>
-
-
</table>
-
-
<br>Dump options(CSV):
-
-
<table border="0">
-
-
<tr>
-
-
<td>Delimiter:</td>
-
-
<td><select name="csv_delimiter">
-
-
-
-
-
</select>
-
-
</td>
-
-
</tr>
-
-
<tr>
-
-
<td>Table:</td>
-
-
<td><input type="input" name="mysql_table" value="<?php echo $_REQUEST['mysql_table']; ?>" /></td>
-
-
</tr>
-
-
<tr>
-
-
<td>Header: </td>
-
-
-
</tr>
-
-
</table>
-
-
-
-
-
-
<input type="submit" name="action" value="Export"><br />
-
-
</form>
-
-
</body>
-
-
</html>
-
-
-
-
<?php
-
-
}
-
-
?>
More Reading:
4 Comments
|
thanks man it nice tool but when i want import the mysql with bigdump is not working can u help me man |
|
try this method instead http://www.edmondscommerce.co.uk/blog/mysql/ssh-command-line-mysql-dump-and-compress/ |
|
Hi, http://groups.google.com/group/opensource-malaysia/web/mysqldump.zip |
|
I have examined with new release of the Drupal. It is actually really good. The admin panel is similar to wordpress. And some more functionalities are added together. |
RSS Feed
mario
February 20th, 2009