PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 数据库应用 » ahpche+php备份mysql,解决乱码之忧
本页主题: ahpche+php备份mysql,解决乱码之忧 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

admin



该用户目前不在线
级别: 新手上路
精华: 0
发帖: 0
威望: -1 点
金钱: -10 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2005-09-27
最后登录:2007-08-26

ahpche+php备份mysql,解决乱码之忧


顶端 Posted: 2005-09-21 14:44 | [楼 主]
admin



该用户目前不在线
级别: 新手上路
精华: 0
发帖: 0
威望: -1 点
金钱: -10 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2005-09-27
最后登录:2007-08-26

ahpche+php备份mysql,解决乱码之忧


如果不要备份结构:请屏掉这句://echo get_table_def($dbname, $table, $crlf).";$crlf$crlf";
如果不要备份内容:请屏掉这句:echo get_table_content($dbname, $table, $crlf);


header("Content-disposition: filename=backup.sql");//所保存的文件名
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");

//备份数据

$i = 0;
$crlf="
";
$dbname="movie";//数据库名
global $dbconn;
$dbconn = mysql_connect("localhost","root","pass");//数据库主机,用户名,密码
$db = mysql_select_db($dbname,$dbconn);
$tables = mysql_list_tables($dbname,$dbconn);
$num_tables = @mysql_numrows($tables);
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);

print $crlf;
print $crlf;

echo get_table_def($dbname, $table, $crlf).";$crlf$crlf";
echo get_table_content($dbname, $table, $crlf);
$i++;
}

//定义抽取表结构与数据


function get_table_def($db, $table, $crlf)
{
global $drop;

$schema_create = "";
if(!empty($drop))
$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";

$schema_create .= "CREATE TABLE $table ($crlf";
$result = mysql_db_query($db, "SHOW FIELDS FROM $table");
while($row = mysql_fetch_array($result))
{
$schema_create .= " $row[Field] $row[Type]";

if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",$crlf";
}
$schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
$result = mysql_db_query($db, "SHOW KEYS FROM $table");
while($row = mysql_fetch_array($result))
{
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE|$kname";
if(!isset($index[$kname]))
$index[$kname] = array();
$index[$kname][] = $row['Column_name'];
}

while(list($x, $columns) = @each($index))
{
$schema_create .= ",$crlf";
if($x == "PRIMARY")
$schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else
$schema_create .= " KEY $x (" . implode($columns, ", ") . ")";
}

$schema_create .= "$crlf)";
return (stripslashes($schema_create));
}

//保存数据表的值
function get_table_content($db, $table, $crlf)
{
$schema_create = "";
$temp = "";
$result = mysql_db_query($db, "SELECT * FROM $table");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j {
if(!isset($row[$j]))
$schema_insert .= " NULL,";
elseif($row[$j] != "")
$schema_insert .= " '".addslashes($row[$j])."',";
else
$schema_insert .= " '',";
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ");$crlf";
$temp = $temp.$schema_insert ;
$i++;
}
return $temp;
}
顶端 Posted: 2005-09-21 14:44 | 1 楼
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 数据库应用

时:12-02 08:04 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -