<?php
$db_name="new";
mysql_connect("localhost","root","123456");
mysql_select_db($db_name);
$tb=mysql_list_tables($db_name);
$sql="";
while($query=mysql_fetch_row($tb)){
$sql="";$table_sql="";
$sql.= get_table_fn($query[0]);
get_table_row($query[0]);
// echo $table_sql."/n";
}
$f=fopen($db_name.".sql","w+");
fwrite($f,$sql);
fclose($f);
//备份所有表SQL语名
function get_table_fn($db_name){
$field="CREATE TABLE `$db_name`( /n";
$query=mysql_query("select * from $db_name");
while($row=mysql_fetch_field($query)){
if($row->not_null===1){$null="DEFAULE NULL";}else{$null="NOT NULL";}
if($row->primary_key===1){$key="primary key";}else{$key="";}
if($row->unsigned===1){$unsig="unsigned";}else{$unsig="";}
$field.="`$row->name` $row->type($row->max_length) $null $key $unsig ,/n";
}
$field.=")/n";
return $field;
}
//备份所有表的数剧
function get_table_row($db_name){
$query=mysql_query("select * from $db_name");
$field="";
$num_rows= mysql_num_rows($query);
$num_field=mysql_num_fields($query);
$table_sqls="";
while($row=mysql_fetch_row($query)){
$r= get_table_fd($num_field,$row);
$table_sqls.="insert into `$db_name` values($r)/n";
}
$f=fopen($db_name.".sql","w+");
fwrite($f,$table_sqls);
fclose($f);
}
//获取每张表的字段值
function get_table_fd($num_field,$row){
$r="";
for($i=0;$i<$num_field;$i++){
$r.= "'$row[$i]',";
}
$r=substr($r, 0, strlen($r)-1);
return $r;
}
?>