SoFunction
Updated on 2025-03-09

PHP backup database class sharing

PHP backup database class sharing

Updated: April 14, 2015 11:14:46 Submission: hebedich
This article describes the implementation of MySQL database backup class in php. Share it for your reference. The specific analysis is as follows: This is a very simple way to use php to back up the class file of the mysql database. We just need to simply configure the connection address username and database.

PHP backup database class sharing

<?php
  /**
   *
   * @name php backup database
   * @param string $DbHost Connect to the host
   * @param string $DbUser Username
   * @param string $DbPwd Connection Password
   * @param string $DbName Database to be backed up
   * @param string $saveFileName The file name to be saved, the default file is saved in the current folder, and is distinguished by date
   * @return Null
   * @example backupMySqlData('localhost', 'root', '123456', 'YourDbName');
   *
   */
  function backupMySqlData($DbHost, $DbUser, $DbPwd, $DbName, $saveFileName = '')
  {
    header("Content-type:text/html;charset=utf-8");
    error_reporting(0);
    set_time_limit(0);

    echo 'Data backup, please wait...<br />';

    $link = mysql_connect($DbHost, $DbUser, $DbPwd) or die('Database connection failed: ' . mysql_error());
    mysql_select_db($DbName) or die('Database connection failed: ' . mysql_error());
    mysql_query('set names utf8');
    
    // Declare variables    $isDropInfo   = '';
    $insertSQL   = '';
    $row      = array();
    $tables     = array();
    $tableStructure = array();
    $fileName    = ($saveFileName ? $saveFileName : 'MySQL_data_bakeup_') . date('YmdHis') . '.sql';

    // Enumerate all tables in this database    $res = mysql_query("SHOW TABLES FROM $DbName");
    while ($row = mysql_fetch_row($res)) {

      $tables[] = $row[0];

    }
    mysql_free_result($res);

    // Enumerate all table creation statements    foreach ($tables as $val) {

      $res = mysql_query("show create table $val", $link);
      $row = mysql_fetch_row($res);

      $isDropInfo   = "DROP TABLE IF EXISTS `" . $val . "`;\r\n";
      $tableStructure = $isDropInfo . $row[1] . ";\r\n";

      file_put_contents($fileName, $tableStructure, FILE_APPEND);
      mysql_free_result($res);
    }

    // Enumerate all tables INSERT statement    foreach ($tables as $val) {

      $res = mysql_query("select * from $val");

      // Tables without data do not execute insert      while ($row = mysql_fetch_row($res)) {

        $sqlStr = "INSERT INTO `".$val."` VALUES (";

        foreach($row as $v){

          $sqlStr .= "'$v',";

        }
        // Remove the last comma        $sqlStr = substr($sqlStr, 0, strlen($sqlStr) - 1);
        $sqlStr .= ");\r\n";

        file_put_contents($fileName, $sqlStr, FILE_APPEND);
      }
      mysql_free_result($res);
    }

    echo 'The data backup was successful!  ';
  }
 // Call this method  backupMySqlData('localhost', 'root', '123456', 'YouDbName');  
?&gt;

The above is the entire content of this article, I hope you like it.

  • php backup mysql database

Related Articles

  • Analysis of faster implementation method of PHP array deduplication

    This article mainly introduces the faster implementation method of PHP array deduplication. It compares and analyzes various common operation techniques and precautions for PHP to implement array deduplication based on the example form. Friends who need it can refer to it.
    2018-05-05
  • Summary of examples of PHP method to read XML files [DOMDocument and simplexml method]

    This article mainly introduces the method of PHP to read XML files. It summarizes and analyzes the related operation techniques of php based on DOMDocument and simplexml methods for loading, reading and other XML files. Friends who need it can refer to it.
    2019-09-09
  • Example of simple client and server communication function implemented by PHP socket based on socket

    This article mainly introduces the simple client-server communication function implemented by PHP based on socket, which can realize the function of receiving strings sent by the client and returning to the client after flipping. Friends who need it can refer to it
    2017-07-07
  • php session lock and concurrency

    This article mainly introduces the locking and concurrency of php session. Related phenomena include request blocking, session data loss, and session data not being read. Interested friends can refer to it.
    2016-01-01
  • PHP obtains the real IP of the real client

    This article mainly introduces the method of PHP to obtain the real IP (REMOTE_ADDR, HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR) of the real client, which is of good reference value. Let's take a look with the editor below
    2017-03-03
  • Simple implementation of the method to limit phpmyadmin access to IP

    If you need to limit phpmyadmin specific IP address segments for access, a simple way can be simply qualified in the configuration file.
    2013-03-03
  • Methods for executing cmd commands in PHP

    This article mainly introduces the method of executing cmd command in PHP. Friends who need it can refer to it
    2014-10-10
  • Example of method for php regular deletion of img tags

    This article mainly introduces the method of php to delete img tags regularly, and analyzes the operation techniques related to php's regular matching of img tags based on specific examples. Friends who need it can refer to it
    2017-05-05
  • Example of PHP send email confirmation verification registration function [Modify other people's email category]

    This article mainly introduces the PHP email confirmation verification and registration function, and analyzes the modification and usage skills of PHP open source email operation in combination with examples. Friends who need it can refer to it
    2019-11-11
  • php_imagick method to achieve image cutting, rotation, sharpening, subtracting color or adding special effects

    This article mainly introduces the method of php_imagick to cut, rotate, sharpen, subtract color or increase special effects. It can realize the PHP extension of ImageMagick function to enable PHP to have the same functions as ImageMagick, and ultimately realize the powerful ImageMagick graphics processing function. It is very practical. Friends who need it can refer to it.
    2014-12-12

Latest Comments