hg888皇冠手机登录

PHP备份数据库为SQL文件

九月 28th, 2019  |  www.hg888.com

例子。

<?php    
$host=”localhost”;       
$user=”root”;    
$password=”123456″;      
$dbname=”nbs”;    
mysql_connect($host,$user,$password);       
mysql_select_db($dbname);     
$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\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”;    
    }    
    $mysql.=”\r\n”;                 
}    
$filename=date(‘Ymj’).”.sql”;       
$fp = fopen($filename,’w’);       
fputs($fp,$mysql);         
fclose($fp);         
echo “<br><center>数据备份成功,生成备份文件”.$filename.”</center>”;           
?>

那是一个将点名数据库里的全数表备份为二个SQL文件,可下载。“`php
<!?php

 

/\** 备份数据库结构* ***/

<?php

 代码如下

  /* 
  函数名称:table2sql() 
  函数功能:把表的结构转换成为SQL 
  函数参数:$table: 要进行提取的表名 
  返 回 值:返回提取后的结果,SQL集合 
  函数作者:heiyeluren 
  */

 function table2sql($table)  
  {  
      global $db;  
     $tabledump = "DROP TABLE IF EXISTS $table;\n";  
     $createtable = $db--->query("SHOW CREATE TABLE $table");  
     $create = $db->fetch_row($createtable);  
     $tabledump .= $create[1].";\n\n";  
      return $tabledump;  
  }

 /****** 备份数据库结构和所有数据 ******/ 
  /* 
  函数名称:data2sql() 
  函数功能:把表的结构和数据转换成为SQL 
  函数参数:$table: 要进行提取的表名 
  返 回 值:返回提取后的结果,SQL集合 
  函数作者:heiyeluren 
  */ 
 function data2sql($table)  
  {  
      global $db;  
     $tabledump = "DROP TABLE IF EXISTS $table;\n";  
     $createtable = $db->query("SHOW CREATE TABLE $table");  
     $create = $db->fetch_row($createtable);  
     $tabledump .= $create[1].";\n\n";

     $rows = $db->query("SELECT * FROM $table");  
     $numfields = $db->num_fields($rows);  
     $numrows = $db->num_rows($rows);  
      while ($row = $db->fetch_row($rows))  
      {  
         $comma = "";  
         $tabledump .= "INSERT INTO $table VALUES(";  
          for($i = 0; $i < $numfields; $i++)  
          {  
             $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";  
             $comma = ",";  
          }  
         $tabledump .= ");\n";  
      }  
     $tabledump .= "\n";

      return $tabledump;  
  }

header(“content-Type: textml; charset=utf-8”);

<?php
// 备份数据库
$host = “localhost”;
$user = “root”; //数据库账号
$password = “”; //数据库密码
$dbname = “mysql”; //数据库名称
// 这里的账号、密码、名称都以从页面传过来的
if (!mysql_connect($host, $user, $password)) // 连接mysql数据库
    {
        echo ‘数据库连接战败,请核查后再试’;
    exit;
}
if
(!mysql_select_db($dbname))
// 是或不是留存该数据库
    {
        echo ‘海市蜃楼数据库:’ . $dbname . ‘,请核对后再试’;
    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 = $dbname . date(‘Ymjgi’) . “.sql”;
//寄存路线,暗中同意贮存到花色最外层
$fp = fopen($filename, ‘w’);
fputs($fp, $mysql);
fclose($fp);
echo “数据备份成功”;
 
?>

?>

//备份数据库

例子

<!–?php
$host=”localhost”; //主机名
$user=”root”; //MYSQL用户名
$password=”root”; //密码
$dbname=”dedecmsv4″; //备份的数据库

$host=”192.168.1.149″;

 代码如下

mysql_connect($host,$user,$password);
mysql_select_db($dbname);

$user=”root”;//数据库账号

#!/usr/bin/php www.111cn.net
<?php
header(‘Content-Type:text/html;charset=utf-8’);

$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\r\n”;#DDL

$password=”root”;//数据库密码

define(‘BACK_PATH’, dirname(__FILE__));

$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.”‘”;

$dbname=”weixin”;//数据库名称 //这里的账号、密码、名称都是从页面传过来的

/**
为空数组就备份全体数据库
*/
$back_databases = array(‘learning’,’wp’);

$mysql.=”insert into $table($keys) values($vals);\r\n”;
}
$mysql.=”\r\n”;

$pdo=newPDO(“mysql:host=$host;dbname=$dbname”,$user,$password);

$mysql_host = ‘localhost’;

}
$filename=date(‘Ymd’).”_”.$dbname.”.sql”; //文件名叫当天的日子
$fp = fopen($filename,’w’);
fputs($fp,$mysql);
fclose($fp);
echo “数据备份成功,生成备份文件”.$filename;
?> “`

if(!$pdo)//连接mysql数据库

$mysql_user = ‘root’;

{

$mysql_passwd = ‘root’;

echo’数据库连接战败,请查对后再试’;exit;

标签:, , ,

Your Comments

近期评论

    功能


    网站地图xml地图