php - 备份mysql数据库取特定表
问题描述
你好这是我用来备份mysql数据库的php代码,它需要所有的数据库和所有的表,我想在数据库中获取一个特定的表而不是所有的表。可能有人请告诉我如何修改 php 代码以获取单个表而不是所有表?这是代码:
<?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') 放在表格 array[foreach($tables as $table)] 的循环之前,我现在可以工作,但它给了我一个错误:注意:未定义的变量:返回。请问有人可以帮我找出问题所在吗?如何解决?
解决方案
在表格数组的循环之前,将数组设置为你要抓取的表格。
$tables = ['tale1','table2','table3'];
foreach($tables as $table)
删除被注释的代码块//get all of the tables
推荐阅读
- c# - Blazor.net 不需要的按钮单击输入按键
- java - 使用 Robolectric 对 Toast 消息内容进行单元测试
- angular - 当 Ionic 3 中的 Firestore 集合发生更改时,如何实现应用程序范围的通知功能?
- google-apps-script - 无法使用 google-apps-script 获取 json 数据
- sql-server - 我如何在数字 0.00 中执行 SQL Server 数据类型货币
- angular - Angular 8 Reactive Form 动态编辑/添加模式验证
- vue.js - 上传到heroku服务器后网页高度发生变化
- html - 想要在父列的大小内限制内容的大小 [twitter bootstrap]
- python - 无法使用 Python 请求或 Urllib 模块读取网站
- python - 从点 Django 获取坐标