用PHP下载MySql多表

用PHP下载MySql多表,php,mysql,download,Php,Mysql,Download,我正在尝试将自定义功能添加到我正在处理的站点的后端 我有一个MySql数据库,其中包含多个表,我希望通过单击链接可以下载这些表 如果备份文件也可以添加到zip文件中进行下载,那就太好了 那么,在PHP中,如何创建所有数据库表的副本(最好是SQL格式)并将其压缩以供下载 我不确定MySql的“入出文件”方法是否能在我的共享主机帐户上工作。MySql数据库与我的站点文件位于不同的服务器上。请尝试以下代码。可能需要进行一些调试,但您会明白这一点 backup_tables('localhost','u

我正在尝试将自定义功能添加到我正在处理的站点的后端

我有一个MySql数据库,其中包含多个表,我希望通过单击链接可以下载这些表

如果备份文件也可以添加到zip文件中进行下载,那就太好了

那么,在PHP中,如何创建所有数据库表的副本(最好是SQL格式)并将其压缩以供下载


我不确定MySql的“入出文件”方法是否能在我的共享主机帐户上工作。MySql数据库与我的站点文件位于不同的服务器上。

请尝试以下代码。可能需要进行一些调试,但您会明白这一点

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);
}

我可能要到今天晚些时候才能测试这个,但我会让你知道它是如何运行的。你发布的第一个代码是有效的,我现在可以得到我的表的副本了。还没有试过第二部分。你试过INTO-OUTFILE方法吗?如果没有,您是否可以通过PHP访问命令行上的mysqldump?
/* 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;
    }
}