簡體   English   中英

備份mysql數據庫取特定表

[英]Backup mysql Database take a specific table

您好,這是我用來備份mysql DATABASE的php代碼,它包含所有DATABASE和所有表,我想獲取數據庫中的特定表而不是所有表。 可能有人可以告訴我如何修改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)]的循環之前,我現在無法正常工作,但它給了我一個錯誤:注意:未定義的變量:return。 請有人可以幫助我找出問題所在嗎? 如何解決?

在表數組循環之前,將數組設置為要獲取的表。

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

刪除注釋的代碼塊//get all of the tables

Bravooooooooooooooo! 我修復了它,謝謝#user3720435
步驟1:我刪除了注釋的代碼塊//獲取所有表
第2步:
我將$ tables = array('myTable')放在表array [foreach($ tables as $ table)]循環之前,我現在無法正常工作,但它給了我一個錯誤。 注意:未定義的變量:返回。
步驟3:我通過將return = null來解決它; 被注釋的代碼塊之前//表格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM