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

PHP 对 MySQL 数据库进行备份

PHP中文网
PHP中文网原创
2016-05-25 17:15:511055浏览

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;
      }
?>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn