2017-02-09 107 views
0

我在PHP中尋找一種方式將mysql數據庫作爲.sql文件發送到客戶端作爲下載。 我發現了一堆解決方案,我非常喜歡通過exec()(或shell_exec()system())使用mysqldump命令的方法,但遺憾的是在這種情況下mysqldump命令不適用於我。由於我的數據庫連接不能被硬編碼,我發現了很多'重新發明輪子'的解決方案,在那裏你只需查詢'SHOW TABLES'並開始迭代,就像在這裏:Export MySQL database using PHP only(Raju Dudhrejiya的答案)。 這工作完全正常,直到我注意到它只有保存表,以及我發現的每個其他類似的方法。沒有事件,觸發器等,這是不好的。特別糟糕。PHP下載完整的mysql數據庫爲.sql備份文件

我需要的東西至少也會支持事件,所以我的第一種方法是擴展以下方法(從上面的鏈接中獲取),儘管我不知道如何繼續。我想我必須以某種方式查詢所有事件並在某處添加'CREATE EVENT'聲明? 有沒有人做過類似的事情?

function Export_Database($host,$user,$pass,$name, $tables=false, $backup_name=false) 
    { 
     $mysqli = new mysqli($host,$user,$pass,$name); 
     $mysqli->select_db($name); 
     $mysqli->query("SET NAMES 'utf8'"); 

    $queryTables = $mysqli->query('SHOW TABLES'); 
    while($row = $queryTables->fetch_row()) 
    { 
     $target_tables[] = $row[0]; 
    } 
    if($tables !== false) 
    { 
     $target_tables = array_intersect($target_tables, $tables); 
    } 
    foreach($target_tables as $table) 
    { 
     $result   = $mysqli->query('SELECT * FROM '.$table); 
     $fields_amount = $result->field_count; 
     $rows_num=$mysqli->affected_rows;  
     $res   = $mysqli->query('SHOW CREATE TABLE '.$table); 
     $TableMLine  = $res->fetch_row(); 
     $content  = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n"; 

     for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) 
     { 
      while($row = $result->fetch_row()) 
      { //when started (and every after 100 command cycle): 
       if ($st_counter%100 == 0 || $st_counter == 0) 
       { 
         $content .= "\nINSERT INTO ".$table." VALUES"; 
       } 
       $content .= "\n("; 
       for($j=0; $j<$fields_amount; $j++) 
       { 
        $row[$j] = str_replace("\n","\\n", addslashes($row[$j])); 
        if (isset($row[$j])) 
        { 
         $content .= '"'.$row[$j].'"' ; 
        } 
        else 
        { 
         $content .= '""'; 
        }  
        if ($j<($fields_amount-1)) 
        { 
          $content.= ','; 
        }  
       } 
       $content .=")"; 
       //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler 
       if ((($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
       { 
        $content .= ";"; 
       } 
       else 
       { 
        $content .= ","; 
       } 
       $st_counter=$st_counter+1; 
      } 
     } $content .="\n\n\n"; 
    } 
    //$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql"; 
    $backup_name = $backup_name ? $backup_name : $name.".sql"; 
    header('Content-Type: application/octet-stream'); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"".$backup_name."\""); 
    echo $content; exit; 
} 

回答

0

沒關係,我自己想清楚了。

這增加了事件表,觸發器/存儲過程應該以類似的方式工作:

function Export_Database($host,$user,$pass,$name,$tables=false,$backup_name=false){ 
    $mysqli = new mysqli($host,$user,$pass,$name); 
    $mysqli->select_db($name); 
    $mysqli->query("SET NAMES 'utf8'"); 
    $queryTables = $mysqli->query("SHOW TABLES"); 
    while($row = $queryTables->fetch_row()){ 
    $target_tables[] = $row['0']; //put each table name into array 
    } 
    if($tables){ 
    $target_tables = array_intersect($target_tables, $tables); 
    } 
    foreach($target_tables as $table){ 
    $result   = $mysqli->query('SELECT * FROM '.$table); 
    $fields_amount = $result->field_count; 
    $rows_num  = $mysqli->affected_rows; 
    $res   = $mysqli->query('SHOW CREATE TABLE '.$table); 
    $TableMLine  = $res->fetch_row(); 
    $content  = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n"; 

    for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0){ 
     while($row = $result->fetch_row()){ 
     //when started (and every after 100 command cycle): 
     if ($st_counter%100 == 0 || $st_counter == 0){ 
      $content .= "\nINSERT INTO ".$table." VALUES"; 
     } 
     $content .= "\n("; 
     for($j=0; $j<$fields_amount; $j++){ 
      $row[$j] = str_replace("\n","\\n", addslashes($row[$j])); 
      if (isset($row[$j])){ 
      $content .= '"'.$row[$j].'"' ; 
      } else { 
      $content .= '""'; 
      } 
      if ($j<($fields_amount-1)){ 
      $content.= ','; 
      } 
     } 
     $content .=")"; 
     //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler 
     if ((($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num){ 
      $content .= ";"; 
     } else { 
      $content .= ","; 
     } 
     $st_counter=$st_counter+1; 
     } 
    } $content .="\n\n\n"; 
    } 

    $events = $mysqli->query("SHOW EVENTS"); 
    while($events && ($row = $events->fetch_row())){ 
    $res = $mysqli->query("SHOW CREATE EVENT ".$row[0].'.'.$row[1]); 
    $TableMLine = $res->fetch_row(); 
    $content .= "\n\n".$TableMLine[3].";\n\n"; 
    } 

    $backup_name = $backup_name ? $backup_name : $name.".sql"; 
    header('Content-Type: application/octet-stream'); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"".$backup_name."\""); 
    echo $content; exit; 
} 
+0

改成了適合沒有事件的數據庫 –