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:
  1. <?php
  2.  
  3.  
  4. /*---------------------------------------------------+
  5. | mysqldump.php
  6. +----------------------------------------------------+
  7. | Copyright 2006 Huang Kai
  8. | hkai@atutility.com
  9. | http://atutility.com/
  10. +----------------------------------------------------+
  11. | Released under the terms & conditions of v2 of the
  12. | GNU General Public License. For details refer to
  13. | the included gpl.txt file or visit http://gnu.org
  14. +----------------------------------------------------*/
  15.  
  16. /*
  17. change log:
  18. 2006-10-16 Huang Kai
  19. ---------------------------------
  20. initial release
  21. 2006-10-18 Huang Kai
  22. ---------------------------------
  23. fixed bugs with delimiter
  24. add paramter header to add field name as CSV file header.
  25. 2006-11-11 Huang Kia
  26. Tested with IE and fixed the <button> to <input>
  27. */
  28.  
  29. $mysqldump_version="1.02";
  30.  
  31.  
  32.  
  33. $print_form=1;
  34.  
  35. $output_messages=array();
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //test mysql connection
  42.  
  43. if( isset($_REQUEST['action']) )
  44.  
  45. {
  46.  
  47.     $mysql_host=$_REQUEST['mysql_host'];
  48.  
  49.     $mysql_database=$_REQUEST['mysql_database'];
  50.  
  51.     $mysql_username=$_REQUEST['mysql_username'];
  52.  
  53.     $mysql_password=$_REQUEST['mysql_password'];
  54.  
  55.  
  56.  
  57.     if( 'Test Connection' == $_REQUEST['action'])
  58.  
  59.     {
  60.  
  61.         _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
  62.  
  63.     }
  64.  
  65.     else if( 'Export' == $_REQUEST['action'])
  66.  
  67.     {
  68.  
  69.         _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
  70.  
  71.         if( 'SQL' == $_REQUEST['output_format'] )
  72.  
  73.         {
  74.  
  75.             $print_form=0;
  76.  
  77.  
  78.  
  79.             //ob_start("ob_gzhandler");
  80.  
  81.             header('Content-type: text/plain');
  82.  
  83.             header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('YmdHis').'.sql"');
  84.  
  85.             echo "/*mysqldump.php version $mysqldump_version */\n";
  86.  
  87.             _mysqldump($mysql_database);
  88.  
  89.  
  90.  
  91.             //header("Content-Length: ".ob_get_length());
  92.  
  93.  
  94.  
  95.             //ob_end_flush();
  96.  
  97.         }
  98.  
  99.         else if( 'CSV' == $_REQUEST['output_format'] && isset($_REQUEST['mysql_table']))
  100.  
  101.         {
  102.  
  103.             $print_form=0;
  104.  
  105.  
  106.  
  107.             ob_start("ob_gzhandler");
  108.  
  109.  
  110.  
  111.             header('Content-type: text/comma-separated-values');
  112.  
  113.             header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".$mysql_table."_".date('YmdHis').'.csv"');
  114.  
  115.             //header('Content-type: text/plain');
  116.  
  117.             _mysqldump_csv($_REQUEST['mysql_table']);
  118.  
  119.             header("Content-Length: ".ob_get_length());
  120.  
  121.             ob_end_flush();
  122.  
  123.         }
  124.  
  125.     }
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133. function _mysqldump_csv($table)
  134.  
  135. {
  136.  
  137.     $delimiter= ",";
  138.  
  139.     if( isset($_REQUEST['csv_delimiter']))
  140.  
  141.         $delimiter= $_REQUEST['csv_delimiter'];
  142.  
  143.  
  144.  
  145.     if( 'Tab' == $delimiter)
  146.  
  147.         $delimiter="\t";
  148.  
  149.  
  150.  
  151.  
  152.  
  153.     $sql="select * from `$table`;";
  154.  
  155.     $result=mysql_query($sql);
  156.  
  157.     if( $result)
  158.  
  159.     {
  160.  
  161.         $num_rows= mysql_num_rows($result);
  162.  
  163.         $num_fields= mysql_num_fields($result);
  164.  
  165.  
  166.  
  167.         $i=0;
  168.  
  169.         while( $i <$num_fields)
  170.  
  171.         {
  172.  
  173.             $meta= mysql_fetch_field($result, $i);
  174.  
  175.             echo($meta->name);
  176.  
  177.             if( $i <$num_fields-1)
  178.  
  179.                 echo "$delimiter";
  180.  
  181.             $i++;
  182.  
  183.         }
  184.  
  185.         echo "\n";
  186.  
  187.  
  188.  
  189.         if( $num_rows> 0)
  190.  
  191.         {
  192.  
  193.             while( $row= mysql_fetch_row($result))
  194.  
  195.             {
  196.  
  197.                 for( $i=0; $i <$num_fields; $i++)
  198.  
  199.                 {
  200.  
  201.                     echo mysql_real_escape_string($row[$i]);
  202.  
  203.                     if( $i <$num_fields-1)
  204.  
  205.                             echo "$delimiter";
  206.  
  207.                 }
  208.  
  209.                 echo "\n";
  210.  
  211.             }
  212.  
  213.  
  214.  
  215.         }
  216.  
  217.     }
  218.  
  219.     mysql_free_result($result);
  220.  
  221.  
  222.  
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229. function _mysqldump($mysql_database)
  230.  
  231. {
  232.  
  233.     $sql="show tables;";
  234.  
  235.     $result= mysql_query($sql);
  236.  
  237.     if( $result)
  238.  
  239.     {
  240.  
  241.         while( $row= mysql_fetch_row($result))
  242.  
  243.         {
  244.  
  245.             _mysqldump_table_structure($row[0]);
  246.  
  247.  
  248.  
  249.             if( isset($_REQUEST['sql_table_data']))
  250.  
  251.             {
  252.  
  253.                 _mysqldump_table_data($row[0]);
  254.  
  255.             }
  256.  
  257.         }
  258.  
  259.     }
  260.  
  261.     else
  262.  
  263.     {
  264.  
  265.         echo "/* no tables in $mysql_database */\n";
  266.  
  267.     }
  268.  
  269.     mysql_free_result($result);
  270.  
  271. }
  272.  
  273.  
  274.  
  275. function _mysqldump_table_structure($table)
  276.  
  277. {
  278.  
  279.     echo "/* Table structure for table `$table` */\n";
  280.  
  281.     if( isset($_REQUEST['sql_drop_table']))
  282.  
  283.     {
  284.  
  285.         echo "DROP TABLE IF EXISTS `$table`;\n\n";
  286.  
  287.     }
  288.  
  289.     if( isset($_REQUEST['sql_create_table']))
  290.  
  291.     {
  292.  
  293.  
  294.  
  295.         $sql="show create table `$table`; ";
  296.  
  297.         $result=mysql_query($sql);
  298.  
  299.         if( $result)
  300.  
  301.         {
  302.  
  303.             if($row= mysql_fetch_assoc($result))
  304.  
  305.             {
  306.  
  307.                 echo $row['Create Table'].";\n\n";
  308.  
  309.             }
  310.  
  311.         }
  312.  
  313.         mysql_free_result($result);
  314.  
  315.     }
  316.  
  317. }
  318.  
  319.  
  320.  
  321. function _mysqldump_table_data($table)
  322.  
  323. {
  324.  
  325.  
  326.  
  327.     $sql="select * from `$table`;";
  328.  
  329.     $result=mysql_query($sql);
  330.  
  331.     if( $result)
  332.  
  333.     {
  334.  
  335.         $num_rows= mysql_num_rows($result);
  336.  
  337.         $num_fields= mysql_num_fields($result);
  338.  
  339.  
  340.  
  341.         if( $num_rows> 0)
  342.  
  343.         {
  344.  
  345.             echo "/* dumping data for table `$table` */\n";
  346.  
  347.  
  348.  
  349.             $field_type=array();
  350.  
  351.             $i=0;
  352.  
  353.             while( $i <$num_fields)
  354.  
  355.             {
  356.  
  357.                 $meta= mysql_fetch_field($result, $i);
  358.  
  359.                 array_push($field_type, $meta->type);
  360.  
  361.                 $i++;
  362.  
  363.             }
  364.  
  365.  
  366.  
  367.             //print_r( $field_type);
  368.  
  369.             echo "insert into `$table` values\n";
  370.  
  371.             $index=0;
  372.  
  373.             while( $row= mysql_fetch_row($result))
  374.  
  375.             {
  376.  
  377.                 echo "(";
  378.  
  379.                 for( $i=0; $i <$num_fields; $i++)
  380.  
  381.                 {
  382.  
  383.                     if( is_null( $row[$i]))
  384.  
  385.                         echo "null";
  386.  
  387.                     else
  388.  
  389.                     {
  390.  
  391.                         switch( $field_type[$i])
  392.  
  393.                         {
  394.  
  395.                             case 'int':
  396.  
  397.                                 echo $row[$i];
  398.  
  399.                                 break;
  400.  
  401.                             case 'string':
  402.  
  403.                             case 'blob' :
  404.  
  405.                             default:
  406.  
  407.                                 echo "'".mysql_real_escape_string($row[$i])."'";
  408.  
  409.  
  410.  
  411.                         }
  412.  
  413.                     }
  414.  
  415.                     if( $i <$num_fields-1)
  416.  
  417.                         echo ",";
  418.  
  419.                 }
  420.  
  421.                 echo ")";
  422.  
  423.  
  424.  
  425.                 if( $index <$num_rows-1)
  426.  
  427.                     echo ",";
  428.  
  429.                 else
  430.  
  431.                     echo ";";
  432.  
  433.                 echo "\n";
  434.  
  435.  
  436.  
  437.                 $index++;
  438.  
  439.             }
  440.  
  441.         }
  442.  
  443.     }
  444.  
  445.     mysql_free_result($result);
  446.  
  447.     echo "\n";
  448.  
  449. }
  450.  
  451.  
  452.  
  453. function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
  454.  
  455. {
  456.  
  457.     global $output_messages;
  458.  
  459.     $link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
  460.  
  461.     if (!$link)
  462.  
  463.     {
  464.  
  465.        array_push($output_messages, 'Could not connect: ' . mysql_error());
  466.  
  467.     }
  468.  
  469.     else
  470.  
  471.     {
  472.  
  473.         array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");
  474.  
  475.  
  476.  
  477.         $db_selected = mysql_select_db($mysql_database, $link);
  478.  
  479.         if (!$db_selected)
  480.  
  481.         {
  482.  
  483.             array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
  484.  
  485.         }
  486.  
  487.         else
  488.  
  489.             array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
  490.  
  491.     }
  492.  
  493.  
  494.  
  495. }
  496.  
  497.  
  498.  
  499. if( $print_form>0 )
  500.  
  501. {
  502.  
  503. ?>
  504.  
  505. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  506.  
  507.  
  508.  
  509. <html>
  510.  
  511. <head>
  512.  
  513. <title>mysqldump.php version <?php echo $mysqldump_version; ?></title>
  514.  
  515. </head>
  516.  
  517.  
  518.  
  519. <body>
  520.  
  521. <?php
  522.  
  523.     foreach ($output_messages as $message)
  524.  
  525.     {
  526.  
  527.         echo $message."<br />";
  528.  
  529.     }
  530.  
  531. ?>
  532.  
  533. <form action="" method="post">
  534.  
  535. MySQL connection parameters:
  536.  
  537. <table border="0">
  538.  
  539.   <tr>
  540.  
  541.     <td>Host:</td>
  542.  
  543.     <td><input  name="mysql_host" value="<?php if(isset($_REQUEST['mysql_host']))echo $_REQUEST['mysql_host']; else echo 'localhost';?>"  /></td>
  544.  
  545.   </tr>
  546.  
  547.   <tr>
  548.  
  549.     <td>Database:</td>
  550.  
  551.     <td><input  name="mysql_database" value="<?php echo $_REQUEST['mysql_database']; ?>"  /></td>
  552.  
  553.   </tr>
  554.  
  555.   <tr>
  556.  
  557.     <td>Username:</td>
  558.  
  559.     <td><input  name="mysql_username" value="<?php echo $_REQUEST['mysql_username']; ?>"  /></td>
  560.  
  561.   </tr>
  562.  
  563.   <tr>
  564.  
  565.     <td>Password:</td>
  566.  
  567.     <td><input  type="password" name="mysql_password" value="<?php echo $_REQUEST['mysql_password']; ?>"  /></td>
  568.  
  569.   </tr>
  570.  
  571.   <tr>
  572.  
  573.     <td>Output format: </td>
  574.  
  575.     <td>
  576.  
  577.       <select name="output_format">
  578.  
  579.         <option value="SQL" <?php if( isset($_REQUEST['output_format']) && 'SQL' == $_REQUEST['output_format']) echo "selected";?>>SQL</option>
  580.  
  581.         <option value="CSV" <?php if( isset($_REQUEST['output_format']) && 'CSV' == $_REQUEST['output_format']) echo "selected";?>>CSV</option>
  582.  
  583.  
  584.  
  585.         </select>
  586.  
  587.     </td>
  588.  
  589.   </tr>
  590.  
  591. </table>
  592.  
  593. <input type="submit" name="action"  value="Test Connection"><br />
  594.  
  595.  
  596.  
  597.   <br>Dump options(SQL):
  598.  
  599.   <table border="0">
  600.  
  601.  
  602.  
  603.     <tr>
  604.  
  605.       <td>Drop table statement: </td>
  606.  
  607.       <td><input type="checkbox" name="sql_drop_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_drop_table'])) ; else echo 'checked' ?> /></td>
  608.  
  609.     </tr>
  610.  
  611.     <tr>
  612.  
  613.       <td>Create table statement: </td>
  614.  
  615.       <td><input type="checkbox" name="sql_create_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_create_table'])) ; else echo 'checked' ?> /></td>
  616.  
  617.     </tr>
  618.  
  619.     <tr>
  620.  
  621.       <td>Table data: </td>
  622.  
  623.       <td><input type="checkbox" name="sql_table_data"  <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_table_data'])) ; else echo 'checked' ?>/></td>
  624.  
  625.     </tr>
  626.  
  627.   </table>
  628.  
  629.   <br>Dump options(CSV):
  630.  
  631.   <table border="0">
  632.  
  633.   <tr>
  634.  
  635.     <td>Delimiter:</td>
  636.  
  637.     <td><select name="csv_delimiter">
  638.  
  639.       <option value="," <?php if( isset($_REQUEST['output_format']) && ',' == $_REQUEST['output_format']) echo "selected";?>>,</option>
  640.  
  641.       <option value="Tab" <?php if( isset($_REQUEST['output_format']) && 'Tab' == $_REQUEST['output_format']) echo "selected";?>>Tab</option>
  642.  
  643.       <option value="|" <?php if( isset($_REQUEST['output_format']) && '|' == $_REQUEST['output_format']) echo "selected";?>>|</option>
  644.  
  645.     </select>
  646.  
  647.     </td>
  648.  
  649.   </tr>
  650.  
  651.   <tr>
  652.  
  653.     <td>Table:</td>
  654.  
  655.     <td><input  type="input" name="mysql_table" value="<?php echo $_REQUEST['mysql_table']; ?>"  /></td>
  656.  
  657.   </tr>
  658.  
  659.     <tr>
  660.  
  661.       <td>Header: </td>
  662.  
  663.       <td><input type="checkbox" name="csv_header"  <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['csv_header'])) ; else echo 'checked' ?>/></td>
  664.  
  665.     </tr>
  666.  
  667. </table>
  668.  
  669.  
  670.  
  671.  
  672.  
  673. <input type="submit" name="action"  value="Export"><br />
  674.  
  675. </form>
  676.  
  677. </body>
  678.  
  679. </html>
  680.  
  681.  
  682.  
  683. <?php
  684.  
  685. }
  686.  
  687. ?>

More Reading:

  • no matching posts found..
4 Comments

mario
February 20th, 2009

thanks man it nice tool but when i want import the mysql with bigdump is not working can u help me man

 

admin
February 20th, 2009
 

James
January 20th, 2010

Hi,
I've done some modification on it to support views as well.
And for some reason, the ' charactor wasnt escaped in my sql dump. i think it could be reason with usage of utf8 in the data...
So i changed the data delimiter to " instead of '...
and pushed view schema to the last section of the dump...

http://groups.google.com/group/opensource-malaysia/web/mysqldump.zip

 

Parthenia Sveen
May 3rd, 2010

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.

 

 

Leave a Reply