简体   繁体   English

备份mysql数据库取特定表

[英]Backup mysql Database take a specific table

Hello This is the php code I use to Backup a mysql DATABASE, It takes all the DATABASE and all the tables, I want to take a particular table in the database instead of all tables. 您好,这是我用来备份mysql DATABASE的php代码,它包含所有DATABASE和所有表,我想获取数据库中的特定表而不是所有表。 May someOne please tell me How to modify the php code PLEASE in order to take a single table instead of all tables? 可能有人可以告诉我如何修改php代码以获取单个表而不是所有表吗? THis is the code: 这是代码:

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

I put $tables= array('myTable') Before the loop of the tables array[foreach($tables as $table)] , i'ts working now but it's giving me a error: Notice: Undefined variable: return. 我将$ tables = array('myTable')放在表array [foreach($ tables as $ table)]的循环之前,我现在无法正常工作,但它给了我一个错误:注意:未定义的变量:return。 Please can SomeOne help me figure out what's wrong? 请有人可以帮助我找出问题所在吗? How to fix it? 如何解决?

Before the loop of the tables array, set the array to the table you want to grab. 在表数组循环之前,将数组设置为要获取的表。

$tables = ['tale1','table2','table3'];
foreach($tables as $table)

Remove the code block that is commented //get all of the tables 删除注释的代码块//get all of the tables

Bravooooooooooooooo!!! Bravooooooooooooooo! I fixed it thank you #user3720435 我修复了它,谢谢#user3720435
Step 1: I Removed the code block that is commented //get all of the tables 步骤1:我删除了注释的代码块//获取所有表
Step 2: 第2步:
I put $tables= array('myTable') Before the loop of the tables array[foreach($tables as $table)] , i'ts working now but it was giving me an error. 我将$ tables = array('myTable')放在表array [foreach($ tables as $ table)]循环之前,我现在无法正常工作,但它给了我一个错误。 Notice: Undefined variable: return. 注意:未定义的变量:返回。
Step 3: i fix it by putting return=null; 步骤3:我通过将return = null来解决它; before code block that is commented //Over tables 被注释的代码块之前//表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM