SoFunction
Updated on 2025-03-10

php simple backup and restore MySql

This article describes the simple backup and restore of MySql in PHP. Share it for your reference, as follows:

1. Backup:

<?php
header ( "content-Type: text/html; charset=utf-8" );
//Back up the database$host="localhost";
$user="root";//Database account$password="123456";//Database Password$dbname="test";//Database name//The account, password and name here are all transmitted from the pageif(!mysql_connect($host,$user,$password)) //Connect the mysql database{
 echo 'The database connection failed, please check and try again';
 exit;
}
if(!mysql_select_db($dbname)) //Is the database exist?{
 echo 'The database does not exist:'.$dbname.', please check and try again';
 exit;
}
mysql_query("set names 'utf8'");
$mysql= "set charset utf8;\r\n";
$q1=mysql_query("show tables");
while($t=mysql_fetch_array($q1)){
  $table=$t[0];
  $q2=mysql_query("show create table `$table`");
  $sql=mysql_fetch_array($q2);
  $mysql.=$sql['Create Table'].";\r\n";
  $q3=mysql_query("select * from `$table`");
  while($data=mysql_fetch_assoc($q3)){
    $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);\r\n";
  }
}
$filename="data/".$('Ymjgi').".sql"; //Storage path, stored at the outermost layer of the project by default$fp = fopen($filename,'w');
fputs($fp,$mysql);
fclose($fp);
echo "Data backup was successful";
?>

2. Restore

<!--
 author:jelly
 qq:52091199
 blog:.
-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
$filename = "";
$host="localhost"; //Host name$user="root"; //MYSQL username$password="123456"; //password$dbname="test"; //Specify the database name you want to recover here. If it does not exist, it must be created first. Please modify the database name yourself.mysql_connect($host,$user,$password);
mysql_select_db($dbname);
$mysql_file="data/".$filename; //Specify the MySQL backup file path to be restored, please modify this path yourselfrestore($mysql_file); //Execute MySQL recovery commandfunction restore($fname)
 {
 if (file_exists($fname)) {
  $sql_value="";
  $cg=0;
  $sb=0;
  $sqls=file($fname);
  foreach($sqls as $sql)
  {
  $sql_value.=$sql;
  }
  $a=explode(";\r\n", $sql_value); //Execute the database in segments according to the ";\r\n" condition  $total=count($a)-1;
  mysql_query("set names 'utf8'");
  for ($i=0;$i<$total;$i++)
  {
  mysql_query("set names 'utf8'");
  //Execute the command  if(mysql_query($a[$i]))
  {
   $cg+=1;
  }
  else
  {
   $sb+=1;
   $sb_command[$sb]=$a[$i];
  }
  }
  echo "The operation is completed, a total of $total commands are processed, the $cg commands are successful, and the $sb commands are failed";
  //Show error message  if ($sb>0)
  {
  echo "<hr><br><br>The failed command is as follows:<br>";
  for ($ii=1;$ii&lt;=$sb;$ii++)
  {
   echo "<p><b>Part".$ii."A command (content as follows):</b><br>".$sb_command[$ii]."&lt;/p&gt;&lt;br&gt;";
  }
  }  //-----------------------------------------------------------
 }else{
  echo "The MySQL backup file does not exist, please check whether the file path is correct!";
 }
 }
?&gt;

For more information about PHP related content, please check out the topic of this site:Summary of PHP's skills to operate database based on pdo》、《Complete collection of PHP+MongoDB database operation skills》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.