|
<?php
/**
* @desc php,mysql数据库备份
* @author mengdejun
* @date 201011202222
*/
class DbBak
{
var $connection=null;//数据库连接
var $host=null;//主机
var $usr=null;//用户
var $pwd=null;//密码
public function __construct($host,$usr,$pwd,$database=null)
{
$this->host=$host;
$this->usr=$usr;
$this->pwd=$pwd;
$this->connection=mysql_connect($this->host,$this->usr,$this->pwd);
if(!empty($database))mysql_select_db($database,$this->connection);
}
//发送数据库命令
public function send($sql)
{
mysql_query($sql,$this->connection);
}
//枚举数据库表
public function list_tables($database)
{
$rs = mysql_list_tables($database,$this->connection);
$tables = array();
while ($row = mysql_fetch_row($rs))
{
$tables[] = $row[0];
}
mysql_free_result($rs);
return $tables;
}
//导出数据库数据
public function get_table_data($table)
{
$tables=null;
$rs=mysql_query("SELECT * FROM `{$table}`",$this->connection);
while ($row=mysql_fetch_row($rs))
{
$tables.=$this->get_insert_sql($table, $row);
}
mysql_free_result($rs);
return $tables;
}
//导出数据库结构
public function get_table_structure($table)
{
$a=mysql_query("show create table `{$table}`",$this->connection);
$row=mysql_fetch_assoc($a);
return $row['Create Table'].';';//导出表结构
}
//获取insert语句
private function get_insert_sql($table,$row)
{
$sql = "INSERT INTO `{$table}` VALUES (";
$values = array();
foreach ($row as $value)
{
$values[] = "'".mysql_real_escape_string($value)."'";
}
$sql.=implode(',',$values).");";
return $sql;
}
}
?>
<?php
require'DbBak.cls.php';
$db=new DbBak("localhost","root","jack","dedecmsv56utf");
fopen("a.sql","w");
echo $db->get_table_structure("dede_stepselect");
echo $db->get_table_data("dede_stepselect");
?> |
|
|