Advertisement
meetsos

Export MySQL database using PHP only

Jun 8th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <?php
  2.     //ENTER THE RELEVANT INFO BELOW
  3.     $mysqlUserName      = "Your Username";
  4.     $mysqlPassword      = "Your Password";
  5.     $mysqlHostName      = "Your Host";
  6.     $DbName             = "Your Database Name here";
  7.     $backup_name        = "mybackup.sql"; // no need to edit if false
  8.     $tables             = "Your tables"; // no need to edit if false
  9.  
  10.    //or add 5th parameter(array) of specific tables:    array("mytable1","mytable2","mytable3") for multiple tables
  11.  
  12.     Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );
  13.  
  14.     function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false )
  15.     {
  16.         $mysqli = new mysqli($host,$user,$pass,$name);
  17.         $mysqli->select_db($name);
  18.         $mysqli->query("SET NAMES 'utf8'");
  19.  
  20.         $queryTables    = $mysqli->query('SHOW TABLES');
  21.         while($row = $queryTables->fetch_row())
  22.         {
  23.             $target_tables[] = $row[0];
  24.         }  
  25.         if($tables !== false)
  26.         {
  27.             $target_tables = array_intersect( $target_tables, $tables);
  28.         }
  29.         foreach($target_tables as $table)
  30.         {
  31.             $result         =   $mysqli->query('SELECT * FROM '.$table);  
  32.             $fields_amount  =   $result->field_count;  
  33.             $rows_num=$mysqli->affected_rows;    
  34.             $res            =   $mysqli->query('SHOW CREATE TABLE '.$table);
  35.             $TableMLine     =   $res->fetch_row();
  36.             $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";
  37.  
  38.             for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0)
  39.             {
  40.                 while($row = $result->fetch_row())  
  41.                 { //when started (and every after 100 command cycle):
  42.                     if ($st_counter%100 == 0 || $st_counter == 0 )  
  43.                     {
  44.                             $content .= "\nINSERT INTO ".$table." VALUES";
  45.                     }
  46.                     $content .= "\n(";
  47.                     for($j=0; $j<$fields_amount; $j++)  
  48.                     {
  49.                         $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
  50.                         if (isset($row[$j]))
  51.                         {
  52.                             $content .= '"'.$row[$j].'"' ;
  53.                         }
  54.                         else
  55.                         {  
  56.                             $content .= '""';
  57.                         }    
  58.                         if ($j<($fields_amount-1))
  59.                         {
  60.                                 $content.= ',';
  61.                         }      
  62.                     }
  63.                     $content .=")";
  64.                     //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
  65.                     if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
  66.                     {  
  67.                         $content .= ";";
  68.                     }
  69.                     else
  70.                     {
  71.                         $content .= ",";
  72.                     }
  73.                     $st_counter=$st_counter+1;
  74.                 }
  75.             } $content .="\n\n\n";
  76.         }
  77.         //$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
  78.         $backup_name = $backup_name ? $backup_name : $name.".sql";
  79.         header('Content-Type: application/octet-stream');  
  80.         header("Content-Transfer-Encoding: Binary");
  81.         header("Content-disposition: attachment; filename=\"".$backup_name."\"");  
  82.         echo $content; exit;
  83.     }
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement