xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
echo'
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
';
echo'
';
echo'
';
if(is_array($mapping)) {
foreach($mapping as $key=>$val)
echo'
'.$val.' | ';
}
echo'
';
$results=$this->findBySql('select * from '.$table);
foreach($results as $result) {
echo'
';
foreach($result as $key=>$val)
echo'
'.$val.' | ';
echo'
';
}
echo'
';
echo'';
echo'';
}
function Backup($table) {
if(is_array ($table)) {
$str="";
foreach($table as $tab)
$str.=$this->get_table_content($tab);
return $str;
}else {
return $this->get_table_content($table);
}
}
/***************************************************************************
* 备份数据库数据到文件
* $table:表名
* $file:文件名
**************************************************************************/
function Backuptofile($table,$file) {
header("Content-disposition: filename= $file.sql");//所保存的文件名
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
if(is_array ($table)) {
$str="";
foreach($table as $tab)
$str.=$this->get_table_content($tab);
echo $str;
}else {
echo $this->get_table_content($table);
}
}
function Restore($table,$file="",$content="") {
//排除file,content都为空或者都不为空的情况
if(($file==""&&$content=="")||($file!=""&&$content!=""))
echo"参数错误";
$this->truncate($table);
if($file!="") {
if($this->RestoreFromFile($file))
return true;
else
return false;
}
if($content!="") {
if($this->RestoreFromContent($content))
return true;
else
return false;
}
}
//清空表,以便恢复数据
function truncate($table) {
if(is_array ($table)) {
$str="";
foreach($table as $tab)
$this->execute("TRUNCATE TABLE $tab");
}else {
$this->execute("TRUNCATE TABLE $table");
}
}
function get_table_content($table) {
$results=$this->findBySql("select * from $table");
$temp = "";
$crlf="rn";
foreach($results as $result) {
/*(";
foreach( $result as $key=> $val)
{
$schema_insert .= " `". $key."`,";
}
$schema_insert = ereg_replace(", $", "", $schema_insert);
$schema_insert .= ")
*/
$schema_insert = "INSERT INTO $table VALUES (";
foreach($result as $key=>$val) {
if($val != "")
$schema_insert .= " '".addslashes($val)."',";
else
$schema_insert .= "NULL,";
}
$schema_insert = ereg_replace(", $", "", $schema_insert);
$schema_insert .= "); $crlf";
$temp = $temp.$schema_insert ;
}
return $temp;
}
function RestoreFromFile($file) {
if (false !== ($fp = fopen($file, 'r'))) {
$sql_queries = trim(fread($fp, filesize($file)));
$this->splitMySqlFile($pieces, $sql_queries);
foreach ($pieces as $query) {
if(!$this->execute(trim($query)))
return false;
}
return true;
}
return false;
}
function RestoreFromContent($content) {
$content = trim($content);
$this->splitMySqlFile($pieces, $content);
foreach ($pieces as $query) {
if(!$this->execute(trim($query)))
return false;
}
return true;
}
function splitMySqlFile(&$ret, $sql) {
$sql= trim($sql);
$sql=split('',$sql);
$arr=array();
foreach($sql as $sq) {
if($sq!="");
$arr[]=$sq;
}
$ret=$arr;
return true;
}
http://www.bkjia.com/PHPjc/477787.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477787.htmlTechArticle数据库信息导出:word,excel,json,xml,sql 数据库恢复:从sql,从文件 具体用法: 首先新建测试用数据库mytest,然后在里面建张表 PHP代码:...