Multiple ways to back up MySQL data using PHP

Source: Internet
Author: User

  Typically, we use tools to back up, such as phpMyAdmin, Navicat. If you need to use a PHP backup, how do you implement it?

The following two methods are available for research use only.   1th Method: The code is as follows: <?php $host = "localhost"; $user = "root"; $password = ""; $dbname = "dbname";   mysql_connect ($host, $user, $password); mysql_select_db ($dbname); $mysql = "Set names UTF8;"; mysql_query ($mysql); $q 1=mysql_query ("Show Tables"); while ($t =mysql_fetch_array ($q 1)) {$table = $t [0]; $q 2=mysql_query ("Show create Table ' $table '); $sql =mysql_fetch_ Array ($q 2); $mysql. = $sql [' Create Table ']. " n ";   $q 3=mysql_query ("select * from ' $table"); while ($data =mysql_fetch_assoc ($q 3)) {$keys =array_keys ($data); $keys =array_map (' addslashes ', $keys); $keys =join (', ", $keys); $keys = "'. $keys." `"; $vals =array_values ($data); $vals =array_map (' addslashes ', $vals); $vals =join ("', '", $vals); $vals = "'" $vals. "'"; $mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); n"; } $mysql. = "n"; $filename = $dbname. Date (' YMJ '). SQL "; $fp = fopen ($filename, ' w '); Fputs ($fp, $mysql); Fclose ($FP); echo "Data backup succeeded, generate backup file". $filename?>       2nd method:   Code as follows: <?php $host = "LocalhosT "; $user = "root"; $password = ""; $dbname = "dbname"; Backup_tables ($host, $user, $password, $dbname);  /* Backup the DB OR just a table */function Backup_tables ($host, $user, $pass, $name, $tables = ' * ') {    $ link = mysql_connect ($host, $user, $pass);   mysql_select_db ($name, $link);    //get All of the tables   if ($tables = = ' * ')   {    $tables = array ();     $r Esult = mysql_query (' show TABLES ');     while ($row = Mysql_fetch_row ($result))     {      $tables [] = $row [0];   &NBS P }  }   else   {    $tables = Is_array ($tables)? $tables: Explode (', ', $tables);  } &nbsp ; $return = ';  //cycle through   foreach ($tables as $table)   {    $result = mysql_query (' SELECT * from '. $ta BLE);     $num _fields = Mysql_num_fields ($result);         $return. = ' DROP TABLE '. $table. ';     $row 2 = Mysql_fetch_roW (mysql_query (' Show CREATE TABLE '. $table));     $return. = "nn". $row 2[1]. "; nn ";       for ($i = 0; $i < $num _fields $i + +)       {      while ($row = MySQL _fetch_row ($result))       {        $return. = ' INSERT into '. $table. ' VALUES (';         for ($j =0 $j < $num _fields $j + +)           {          $row [$j] = Addslashes ($row [$j]);           $row [$j] = Ereg_replace ("n", "n", $row [$j]);           if (Isset ($row [$j])) {$return. = ' '. $row [$j]. ' ';} else {$return. = ' "';}           if ($j < ($num _fields-1)) {$return. = ', ';}        }   &nbs P     $return. = "); n";      }     {    $return. = "nnn";  }    //save file   $handle = fopen (' db-backup-'. Time (). -'. (MD5 (iMplode (', ', $tables)). " SQL ', ' w+ ');   Fwrite ($handle, $return);   Fclose ($handle); }?>  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.