仅使用PHP导出MySQL数据库[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅使用PHP导出MySQL数据库[关闭]相关的知识,希望对你有一定的参考价值。

有人有一个将mysql数据库导出到.sql文件的教程。我没有shell访问权限所以我不能使用exec我找到了这个网址:http://davidwalsh.name/backup-mysql-database-php但我查看了代码并且这段代码很旧而且编写得不好。我查看了互联网,但对于大多数教程我需要shell访问,其余的已经过时了。

提前致谢。

答案

使用php脚本导出数据库的最佳方法。

或者添加特定表的第五个参数(数组):array("mytable1","mytable2","mytable3")用于多个表

<?php 
    //ENTER THE RELEVANT INFO BELOW
    $mysqlUserName      = "Your Username";
    $mysqlPassword      = "Your Password";
    $mysqlHostName      = "Your Host";
    $DbName             = "Your Database Name here";
    $backup_name        = "mybackup.sql";
    $tables             = "Your tables";

   //or add 5th parameter(array) of specific tables:    array("mytable1","mytable2","mytable3") for multiple tables

    Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );

    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;
    
?>
另一答案

您可以使用此命令它或我100%

shell_exec

注意: exec('C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -uroot DatabaseName> c:\\database_backup.sql'); 是mysqldump app的路径,检查你的电脑。

C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe-uroot

如果你的数据库受密码保护,那么在这个sentense -uUserName之后添加

另一答案

这个工具可能很有用,它是一个基于PHP的纯出口实用程序:https://github.com/2createStudio/shuttle-export

另一答案

请尝试以下方法。

从PHP文件执行数据库备份查询。下面是使用SELECT INTO OUTFILE查询创建表备份的示例:

<?php
$DB_HOST = "localhost";
$DB_USER = "xxx";
$DB_PASS = "xxx";
$DB_NAME = "xxx";

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($con->connect_errno > 0) 
  die('Connection failed [' . $con->connect_error . ']');


$tableName  = 'yourtable';
$backupFile = 'backup/yourtable.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysqli_query($con,$query);
?>

要恢复备份,您只需运行LOAD DATA INFILE查询,如下所示:

<?php
$DB_HOST = "localhost";
$DB_USER = "xxx";
$DB_PASS = "xxx";
$DB_NAME = "xxx";

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($con->connect_errno > 0) 
  die('Connection failed [' . $con->connect_error . ']');


$tableName  = 'yourtable';
$backupFile = 'yourtable.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysqli_query($con,$query);
?>
另一答案

在* nix系统中,使用WHICH命令显示mysqldump的位置,试试这个:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'test';
$mysqldump=exec('which mysqldump');


$command = "$mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $dbname.sql";

exec($command);
?>
另一答案
<?php
 $dbhost = 'localhost:3036';
 $dbuser = 'root';
 $dbpass = 'rootpassword';

 $conn = mysql_connect($dbhost, $dbuser, $dbpass);

 if(! $conn ) 
  die('Could not connect: ' . mysql_error());
 

 $table_name = "employee";
 $backup_file  = "/tmp/employee.sql";
 $sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";

 mysql_select_db('test_db');
 $retval = mysql_query( $sql, $conn );

 if(! $retval ) 
  die('Could not take data backup: ' . mysql_error());
 

 echo "Backedup  data successfully\n";

 mysql_close($conn);
?>
另一答案

我建议你做以下事情,

<?php

function EXPORT_TABLES($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);
    
    $content = "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\r\nSET time_zone = \"+00:00\";\r\n\r\n\r\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\r\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\r\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\r\n/*!40101 SET NAMES utf8 */;\r\n--Database: `" . $name . "`\r\n\r\n\r\n";
    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 .= "\n\n" . $TableMLine[1] . ";\n\n";
        for ($i = 0, $st_counter = 0; $i < $fields_

以上是关于仅使用PHP导出MySQL数据库[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式仅从 node.js 中的数据库(MySQL)中导出特定表? [关闭]

使用php从csv快速更新大量数据mysql [关闭]

从mysql数据库中提取5条随机记录[关闭]

使用php将mysql数据导出到ODF

使用 json rereiver php mysql 在片段中填充列表视图

xml 导出 php 代码仅返回 xml 文件的一个父级,我需要它们