仅使用PHP导出MySQL数据库

有人有一个导出MySQL数据库到.sql文件的教程。 我没有shell访问,所以我不能使用exec我已经find了这个url: http : //davidwalsh.name/backup-mysql-database-php但我看了代码,这个代码是旧的,不好书面。 我已经浏览了互联网,但对于大多数教程我需要shell访问其余的是过时的。

提前致谢。

find这三个环节。 看看它是否可以帮助你

所有这些方法都不使用shell或mysqldump

1.官方的MySQL

2.另一个SQL站点

3.tutorialspoint.com

使用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] ....ps 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('Hi-s')."_".date('dm-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; } ?> 

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

尝试以下。

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

 <?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 [' . $db->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 [' . $db->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); ?> 

我会build议你做下面的事情,

 <?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_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] ....ps 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"; } $content .= "\r\n\r\n/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\r\n/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\r\n/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;"; $backup_name = $backup_name ? $backup_name : $name . "___(" . date('Hi-s') . "_" . date('dm-Y') . ")__rand" . rand(1, 11111111) . ".sql"; header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"" . $backup_name . "\""); echo $content; exit; } ?> 

可以在https://github.com/tazotodua/useful-php-scriptsfind有关导出和导入的enitre项目。;

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

如果你没有phpMyAdmin ,你可以在php CLI命令中写入,例如login到mysql并执行数据库转储。 在这种情况下,您将使用shell_exec函数。

看看这个链接

使用PHP备份您的MySQL数据库

或这一个

PHP类导出/导入MySQL数据库

这里是我的代码,这将备份MySQL数据库并将其存储在指定的path。

 <?php function backup_mysql_database($options){ $mtables = array(); $contents = "-- Database: `".$options['db_to_backup']."` --\n"; $mysqli = new mysqli($options['db_host'], $options['db_uname'], $options['db_password'], $options['db_to_backup']); if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } $results = $mysqli->query("SHOW TABLES"); while($row = $results->fetch_array()){ if (!in_array($row[0], $options['db_exclude_tables'])){ $mtables[] = $row[0]; } } foreach($mtables as $table){ $contents .= "-- Table `".$table."` --\n"; $results = $mysqli->query("SHOW CREATE TABLE ".$table); while($row = $results->fetch_array()){ $contents .= $row[1].";\n\n"; } $results = $mysqli->query("SELECT * FROM ".$table); $row_count = $results->num_rows; $fields = $results->fetch_fields(); $fields_count = count($fields); $insert_head = "INSERT INTO `".$table."` ("; for($i=0; $i < $fields_count; $i++){ $insert_head .= "`".$fields[$i]->name."`"; if($i < $fields_count-1){ $insert_head .= ', '; } } $insert_head .= ")"; $insert_head .= " VALUES\n"; if($row_count>0){ $r = 0; while($row = $results->fetch_array()){ if(($r % 400) == 0){ $contents .= $insert_head; } $contents .= "("; for($i=0; $i < $fields_count; $i++){ $row_content = str_replace("\n","\\n",$mysqli->real_escape_string($row[$i])); switch($fields[$i]->type){ case 8: case 3: $contents .= $row_content; break; default: $contents .= "'". $row_content ."'"; } if($i < $fields_count-1){ $contents .= ', '; } } if(($r+1) == $row_count || ($r % 400) == 399){ $contents .= ");\n\n"; }else{ $contents .= "),\n"; } $r++; } } } if (!is_dir ( $options['db_backup_path'] )) { mkdir ( $options['db_backup_path'], 0777, true ); } $backup_file_name = $options['db_to_backup'] . " sql-backup- " . date( "dmY--his").".sql"; $fp = fopen($backup_file_name ,'w+'); if (($result = fwrite($fp, $contents))) { echo "Backup file created '--$backup_file_name' ($result)"; } fclose($fp); return $backup_file_name; } $options = array( 'db_host'=> 'localhost', //mysql host 'db_uname' => 'root', //user 'db_password' => '', //pass 'db_to_backup' => 'attendance', //database name 'db_backup_path' => '/htdocs', //where to backup 'db_exclude_tables' => array() //tables to exclude ); $backup_file_name=backup_mysql_database($options); 

你可以使用这个命令或者我的作品100%

 exec('C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -uroot DatabaseName> c:\\database_backup.sql'); 

注意:
C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe是mysqldump应用程序的path,请检查您的电脑。

-uroot-u{UserName}

如果你的数据库被密码保护,那么在这个sentense -p{YourPassword}