vertrex

MYSQL Dump Script

Aug 14th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function mysql_to_file($host, $user, $pass, $name, $tables = '*', $to_file = "")
  3. {
  4.     $link = mysql_connect($host, $user, $pass);
  5.     mysql_select_db($name, $link);
  6.    
  7.     //  get all of the tables
  8.     if($tables == '*')
  9.     {
  10.         $tables = array();
  11.         $result = mysql_query('SHOW TABLES', $link);
  12.         while($row = mysql_fetch_row($result))
  13.         {
  14.             $tables[] = $row[0];
  15.         }
  16.     }
  17.     else
  18.     {
  19.         $tables = is_array($tables) ? $tables : explode(',', $tables);
  20.     }
  21.    
  22.     $handle = null;
  23.     if ($to_file == "")
  24.         $handle = fopen($name.'.sql','w+');
  25.     else
  26.         $handle = fopen($to_file,'w+');
  27.        
  28.     fwrite($handle, "--\n");
  29.     fwrite($handle, "-- Database: `'.$name.'`\n");
  30.     fwrite($handle, "--\n\n");
  31.    
  32.     //  loop through the tables
  33.     foreach($tables as $table)
  34.     {
  35.         $return = "";
  36.         $columns = array();
  37.        
  38.         $result = mysql_query('SELECT * FROM `'.$table.'`', $link);
  39.         $num_fields = mysql_num_fields($result);
  40.        
  41.         $return  = "--\n";
  42.         $return .= "-- Table structure for table `".$table."`\n";
  43.         $return .= "--\n\n";
  44.        
  45.         $return.= 'DROP TABLE IF EXISTS `'.$table.'`;'."\n";
  46.         $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$table.'`', $link));
  47.         $return.= "\n".$row2[1].";\n\n";
  48.        
  49.         //  gather the columns for values to inset into
  50.         $column_split = explode("\n", $row2[1]);
  51.         foreach ($column_split as $split_value)
  52.         {            
  53.             $splitString = explode(" ", trim($split_value));
  54.             if ($splitString[0] == "CREATE") continue;
  55.             if ($splitString[0] == "PRIMARY") break;
  56.            
  57.             $columns[count($columns)] = $splitString[0];
  58.         }
  59.        
  60.         $column_names = "";
  61.         foreach ($columns as $key => $name)
  62.         {
  63.             if ($key + 1 == count($columns))
  64.                 $column_names .= $name;
  65.             else
  66.                 $column_names .= $name.', ';
  67.         }
  68.        
  69.         $return .= "--\n";
  70.         $return .= "-- Dumping data for table `".$table."`\n";
  71.         $return .= "--\n\n";
  72.        
  73.         for ($i = 0; $i < $num_fields; $i++)
  74.         {
  75.             while($row = mysql_fetch_row($result))
  76.             {
  77.                 $return.= 'INSERT INTO `'.$table.'` ('.$column_names.') VALUES (';
  78.                 for($j = 0; $j < $num_fields; $j++)
  79.                 {
  80.                     $row[$j] = addslashes($row[$j]);
  81.                     //$row[$j] = preg_replace("\n","\\n",$row[$j]);
  82.                     if (isset($row[$j]))
  83.                     {
  84.                         if (is_numeric($row[$j]))
  85.                             $return.= $row[$j];
  86.                         else
  87.                             $return.= "'".$row[$j]."'";
  88.                     }
  89.                     else
  90.                     {
  91.                         $return.= "''";
  92.                     }
  93.                    
  94.                     if ($j < ($num_fields - 1))
  95.                     {
  96.                         $return.= ', ';
  97.                     }
  98.                 }
  99.                 $return.= ");\n";
  100.             }
  101.         }
  102.         $return.="\n";
  103.         fwrite($handle, $return);
  104.     }
  105.    
  106.     fclose($handle);
  107. }
  108. ?>
Add Comment
Please, Sign In to add comment