<?php
error_reporting(0);
//定义了方法
function get_table_fd($dbname){
$field="CREATE TABLE `$dbname`(\n";
$query=mysql_query("select * from $dbname");
while($meta=mysql_fetch_field($query)){
$field.="`$meta->name` $meta->type($meta->max_length),\n";
}
$field.=")";
return $field;
}
function get_table_row($dbname){
$query=mysql_query("select * from $dbname");
while($row=mysql_fetch_row($query)){
$field.="insert into $dbname values ('$row[0]','$row[1]','$row[2]','$row[3]')\n";
}
$f=fopen($dbname.".sql","w+");
fwrite($f,$field);
fclose($f);
}
?>
以上是配置文件config.php
文件中定义了两个方法函数
get_table_fd()//获取表的属性值后 输出创建表的语句 备份
get_table_row()//获取每个表中的各属性的值 输出插入语句 备份
用到的相关函数:
mysql_list_tables() 表查询函数,类似mysql_query() 函数
mysql_fetch_field() 字段信息函数,返回句柄
下面是mysql_fetch_field() 函数的一些属性 :
Name字段的名称
Table字段所属数据表的名称
Type字段的类型
max_length字段的最大长度
not_null字段是否不能为空,是,则这一项的值为1
primary_key字段是否为主键,是,则这一项的值为1
unique_key字段是否为unique键,是,则这一项的值为1
multiple_key字段是否不为unique键,是,则这一项的值为1
Numeric字段是否为数字型,是,则这一项的值为1
Blob字段是否为blob型,是,则这一项的值为1
Unsigned数字型的字段是否为unsigned的,是,则这一项的值为1
Zerofill数字型的字段是否为zerofilled的,是,则这一项的值为
index.php是运行文件
代码如下:
<?php
include("config.php");
$dbname="mydatabase";
mysql_connect("localhost","root","");
mysql_select_db($dbname);
mysql_query("SET NAMES 'GBK'");
$tq=mysql_list_tables($dbname);
while($tr=mysql_fetch_row($tq)){
$sqlfile.= get_table_fd($tr[0])."\n";
get_table_row($tr[0]);
}
$f=fopen($dbname.".sql","w+");
fwrite($f,$sqlfile);
fclose($f);
?>
运行-备份成功!