备份 mysql 数据库采取特定 table
Backup mysql Database take a specific table
你好,这是我用来备份 mysql 数据库的 php 代码,它需要所有的数据库和所有的 table,我想使用一个特定的 table 在数据库中而不是所有 tables。请有人告诉我如何修改 php 代码以便采用单个 table 而不是所有 table?
这是代码:
<?php
/**
* Updated: Mohammad M. AlBanna
* Website: MBanna.info
*/
//MySQL server and database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'novtech';
$tables = '*';
//Call the core function
backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);
//Core function
function backup_tables($host, $user, $pass, $dbname, $tables = '*') {
$link = mysqli_connect($host,$user,$pass, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
mysqli_query($link, "SET NAMES 'utf8'");
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysqli_query($link, 'SHOW TABLES');
while($row = mysqli_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = '';
//cycle through
foreach($tables as $table)
{
$result = mysqli_query($link, 'SELECT * FROM '.$table);
$num_fields = mysqli_num_fields($result);
$num_rows = mysqli_num_rows($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
$counter = 1;
//Over tables
for ($i = 0; $i < $num_fields; $i++)
{ //Over rows
while($row = mysqli_fetch_row($result))
{
if($counter == 1){
$return.= 'INSERT INTO '.$table.' VALUES(';
} else{
$return.= '(';
}
//Over fields
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
if($num_rows == $counter){
$return.= ");\n";
} else{
$return.= "),\n";
}
++$counter;
}
}
$return.="\n\n\n";
}
//save file , db-backup
//$fileName = 'novtechDB-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$fileName = 'novtechDB'.'.sql';
$handle = fopen($fileName,'w+');
fwrite($handle,$return);
if(fclose($handle)){
echo "Done, the file name is: ".$fileName;
exit;
}
}
我把 $tables= array('myTable') 放在 tables array[foreach($tables as $table)] ,我现在正在工作,但它给了我一个错误:注意:未定义的变量:return。请有人帮我弄清楚出了什么问题吗?如何解决?
tables数组循环之前,将数组设置为你要抓取的table
$tables = ['tale1','table2','table3'];
foreach($tables as $table)
删除注释的代码块//get all of the tables
太棒了!太棒了!
我修好了它
谢谢 #user3720435
第 1 步:
我删除了注释的代码块//get all of the tables
第 2 步:
我把 $tables= array('myTable') 放在 tables array[foreach($tables as $table)] 的循环之前,我现在正在工作,但它给了我一个错误。 注意:未定义变量:return.
第 3 步: 我通过放置 return=null 来修复它;
在被注释的代码块之前 //Over tables
你好,这是我用来备份 mysql 数据库的 php 代码,它需要所有的数据库和所有的 table,我想使用一个特定的 table 在数据库中而不是所有 tables。请有人告诉我如何修改 php 代码以便采用单个 table 而不是所有 table? 这是代码:
<?php
/**
* Updated: Mohammad M. AlBanna
* Website: MBanna.info
*/
//MySQL server and database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'novtech';
$tables = '*';
//Call the core function
backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);
//Core function
function backup_tables($host, $user, $pass, $dbname, $tables = '*') {
$link = mysqli_connect($host,$user,$pass, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
mysqli_query($link, "SET NAMES 'utf8'");
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysqli_query($link, 'SHOW TABLES');
while($row = mysqli_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = '';
//cycle through
foreach($tables as $table)
{
$result = mysqli_query($link, 'SELECT * FROM '.$table);
$num_fields = mysqli_num_fields($result);
$num_rows = mysqli_num_rows($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
$counter = 1;
//Over tables
for ($i = 0; $i < $num_fields; $i++)
{ //Over rows
while($row = mysqli_fetch_row($result))
{
if($counter == 1){
$return.= 'INSERT INTO '.$table.' VALUES(';
} else{
$return.= '(';
}
//Over fields
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
if($num_rows == $counter){
$return.= ");\n";
} else{
$return.= "),\n";
}
++$counter;
}
}
$return.="\n\n\n";
}
//save file , db-backup
//$fileName = 'novtechDB-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$fileName = 'novtechDB'.'.sql';
$handle = fopen($fileName,'w+');
fwrite($handle,$return);
if(fclose($handle)){
echo "Done, the file name is: ".$fileName;
exit;
}
}
我把 $tables= array('myTable') 放在 tables array[foreach($tables as $table)] ,我现在正在工作,但它给了我一个错误:注意:未定义的变量:return。请有人帮我弄清楚出了什么问题吗?如何解决?
tables数组循环之前,将数组设置为你要抓取的table
$tables = ['tale1','table2','table3'];
foreach($tables as $table)
删除注释的代码块//get all of the tables
太棒了!太棒了!
我修好了它
谢谢 #user3720435
第 1 步:
我删除了注释的代码块//get all of the tables
第 2 步:
我把 $tables= array('myTable') 放在 tables array[foreach($tables as $table)] 的循环之前,我现在正在工作,但它给了我一个错误。 注意:未定义变量:return.
第 3 步: 我通过放置 return=null 来修复它;
在被注释的代码块之前 //Over tables