2012-04-14 18 views
0

私が取り組んでいるサイトのバックエンドにカスタム関数を追加しようとしています。PHPを使用したMySqlの複数のテーブルのダウンロード

私はリンクをクリックしてダウンロードできるようにしたい複数のテーブルを持つMySqlデータベースを持っています。

バックアップファイルをすべてダウンロード用のzipファイルに追加することもできます。

PHPでは、データベーステーブルのすべてのコピーを(できればSQL形式で)作成し、ダウンロードするためにそれらを圧縮するにはどうすればよいですか?

MySqlの「INTO OUTFILE」メソッドが自分の共有ホスティングアカウントで機能するかどうかはわかりません。 MySqlデータベースは、自分のサイトのファイルとは別のサーバーにあります。

+0

INTO OUTFILEメソッドを試しましたか?そうでない場合は、コマンドラインでPHP経由でmysqldumpにアクセスできますか? – liquorvicar

答えて

1

このコードを試してください。いくつかのデバッグが必要かもしれませんが、あなたはそのアイデアを得るでしょう。

backup_tables('localhost','username','password','blog'); 


/* backup the db OR just a table */ 
function backup_tables($host,$user,$pass,$name,$tables = '*') 
{ 

    $link = mysql_connect($host,$user,$pass); 
    mysql_select_db($name,$link); 

    //get all of the tables 
    if($tables == '*') 
    { 
     $tables = array(); 
     $result = mysql_query('SHOW TABLES'); 
     while($row = mysql_fetch_row($result)) 
     { 
      $tables[] = $row[0]; 
     } 
    } 
    else 
    { 
     $tables = is_array($tables) ? $tables : explode(',',$tables); 
    } 

    //cycle through 
    foreach($tables as $table) 
    { 
     $result = mysql_query('SELECT * FROM '.$table); 
     $num_fields = mysql_num_fields($result); 

     $return.= 'DROP TABLE '.$table.';'; 
     $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); 
     $return.= "\n\n".$row2[1].";\n\n"; 

     for ($i = 0; $i < $num_fields; $i++) 
     { 
      while($row = mysql_fetch_row($result)) 
      { 
       $return.= 'INSERT INTO '.$table.' VALUES('; 
       for($j=0; $j<$num_fields; $j++) 
       { 
        $row[$j] = addslashes($row[$j]); 
        $row[$j] = ereg_replace("\n","\\n",$row[$j]); 
        if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } 
        if ($j<($num_fields-1)) { $return.= ','; } 
       } 
       $return.= ");\n"; 
      } 
     } 
     $return.="\n\n\n"; 
    } 

    //save file 
    $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+'); 
    fwrite($handle,$return); 
    fclose($handle); 
} 

上記のコードでは、以下の関数を使用することもできますが、zipは使用しません。

/* creates a compressed zip file */ 
function create_zip($files = array(),$destination = '',$overwrite = false) { 
    //if the zip file already exists and overwrite is false, return false 
    if(file_exists($destination) &amp;&amp; !$overwrite) { return false; } 
    //vars 
    $valid_files = array(); 
    //if files were passed in... 
    if(is_array($files)) { 
     //cycle through each file 
     foreach($files as $file) { 
      //make sure the file exists 
      if(file_exists($file)) { 
       $valid_files[] = $file; 
      } 
     } 
    } 
    //if we have good files... 
    if(count($valid_files)) { 
     //create the archive 
     $zip = new ZipArchive(); 
     if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
      return false; 
     } 
     //add the files 
     foreach($valid_files as $file) { 
      $zip->addFile($file,$file); 
     } 
     //debug 
     //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; 

     //close the zip -- done! 
     $zip->close(); 

     //check to make sure the file exists 
     return file_exists($destination); 
    } 
    else 
    { 
     return false; 
    } 
} 
+0

今日まで後でこれをテストすることはできませんが、どのように動くかを教えてくれます。 –

+0

あなたが投稿した最初のコードが動作し、今私のテーブルのコピーを手に入れることができます。まだ2番目の部分をまだ試していない。 –

+0

両方の部品が完璧に動作しました、ありがとう! –

関連する問題