Heim  >  Artikel  >  php教程  >  PHP 对 MySQL 数据库进行备份

PHP 对 MySQL 数据库进行备份

PHP中文网
PHP中文网Original
2016-05-25 17:15:511011Durchsuche

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;
      }
?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn