Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. if (!empty($_POST['dbexport'])) {
  2.     $mysqlUserName = "root";
  3.     $mysqlPassword = "";
  4.     $mysqlHostName = "localhost";
  5.     $DbName = "gestioncle";
  6.     $backup_name = "GestionCle.sql";
  7.     $tables = array("Cles", "Portes", "PretEntreprise", "PretPersonnel", "Users");
  8.  
  9.     Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );
  10.  
  11.     function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false )
  12.     {
  13.         $mysqli = new mysqli($host,$user,$pass,$name);
  14.         $mysqli->select_db($name);
  15.         $mysqli->query("SET NAMES 'utf8'");
  16.  
  17.         $queryTables    = $mysqli->query('SHOW TABLES');
  18.         while($row = $queryTables->fetch_row())
  19.             $target_tables[] = $row[0];
  20.         if($tables !== false)
  21.             $target_tables = array_intersect( $target_tables, $tables);
  22.         foreach($target_tables as $table)
  23.         {
  24.             $result = $mysqli->query('SELECT * FROM '.$table);
  25.             $fields_amount = $result->field_count;
  26.             $rows_num=$mysqli->affected_rows;
  27.             $res = $mysqli->query('SHOW CREATE TABLE '.$table);
  28.             $TableMLine = $res->fetch_row();
  29.             $content = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";
  30.  
  31.             for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0)
  32.             {
  33.                 while($row = $result->fetch_row())
  34.                 {
  35.                     if ($st_counter%100 == 0 || $st_counter == 0 )
  36.                         $content .= "\nINSERT INTO ".$table." VALUES";
  37.                     $content .= "\n(";
  38.                     for($j=0; $j<$fields_amount; $j++)
  39.                     {
  40.                         $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
  41.                         if (isset($row[$j]))
  42.                             $content .= '"'.$row[$j].'"' ;
  43.                         else
  44.                             $content .= '""';
  45.                         if ($j<($fields_amount-1))
  46.                             $content.= ',';
  47.                     }
  48.                     $content .=")";
  49.                     if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
  50.                         $content .= ";";
  51.                     else
  52.                         $content .= ",";
  53.                     $st_counter=$st_counter+1;
  54.                 }
  55.             } $content .="\n\n\n";
  56.         }
  57.         $backup_name = $backup_name ? $backup_name : $name.".sql";
  58.         header('Content-Type: application/octet-stream');
  59.         header("Content-Transfer-Encoding: Binary");
  60.         header("Content-disposition: attachment; filename=\"".$backup_name."\"");
  61.         echo $content; exit;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement