Home >php教程 >PHP源码 >PHP 对 MySQL 数据库进行备份

PHP 对 MySQL 数据库进行备份

PHP中文网
PHP中文网Original
2016-05-25 17:15:511052browse

php代码

<?php
      mysql_connect(&#39;localhost&#39;, &#39;test&#39;, &#39;123456&#39;);
      mysql_select_db(&#39;test&#39;);
      if(copy_table(&#39;products&#39;, &#39;products_bak&#39;)) {
	echo "success\n";
      }
      else {
	echo "failure\n";
      }

      function copy_table($from, $to) {
	if(table_exists($to)) {
	  $success = false;
	}
	else {
	  mysql_query("CREATE TABLE $to LIKE $from");
	  mysql_query("INSERT INTO $to SELECT * FROM $from");
	  $success = true;
        }
        return $success;
      }

      function table_exists($tablename, $database = false) {
	if(!$database) {
	  $res = mysql_query("SELECT DATABASE()");
          $database = mysql_result($res, 0);
        }
        $res = mysql_query("
           SELECT COUNT(*) AS count
           FROM information_schema.tables
           WHERE table_schema = &#39;$database&#39;
           AND table_name = &#39;$tablename&#39;
        ");
        return mysql_result($res, 0) == 1;
      }
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn