您好,欢迎访问一九零五行业门户网

在php中,怎样冲数据库导出表到json/word/xml/excel_PHP教程

数据库信息导出:word,excel,json,xml,sql
  数据库恢复:从sql,从文件
具体用法:
首先新建测试用数据库mytest,然后在里面建张表
php代码:
以下是代码片段:
--  
-- 表的结构 `test` 
--  
create table `test` ( 
  `id` int(11) not null auto_increment, 
  `name` varchar(100) not null, 
  `email` varchar(200) not null, 
  `age` int(3) not null, 
  primary key  (`id`) 
) engine=myisam  default charset=utf8 auto_increment=3 ; 
--  
-- 导出表中的数据 `test` 
--  
insert into `test` (`id`, `name`, `email`, `age`) values  
(1, 'pjq518', [email=]'pjq518@126.com'[/email], 22), 
(2, 'xiaoyu', [email=]'xiaoyu@126.com'[/email], 21);
1.导出ext能方便调用的json
php代码:
以下是代码片段:
 $db=new db(); 
echo  $db->toextjson('test');
//输出结果为
//{'totalcount':'2','rows':[{'id':'1','name':'pjq518','email':'pjq518@126.com','age':'22'},{'id':'2','name':'xiaoyu','email':'xiaoyu@126.com','age':'21'}]}
toextjson( $table, $start=0, $limit=10, $cons=)有4个参数, $table为表名, $cons为条件,可以为string或array
2、导出xml
php代码:
以下是代码片段:
 $db=new db(); 
echo  $db->toextxml('test');
//输出结果
3、导出excel和word
php代码:
以下是代码片段:
 $db=new db(); 
//toexcel 
 $map=array('no','name','email','age');//表头 
 $db->toexcel('test', $map,'档案'); 
//导出word表格 
// $db->toword('test', $map,'档案');
//效果如下图
php代码:
conn=mysql_connect($host,$user,$pass))
            die(can't connect to mysql sever);
        mysql_select_db($db,$this->conn);
        mysql_query(set names 'utf-8');
    }
/***************************************************************************
     * 执行sql查询
     * return:查询结构集 resource
     **************************************************************************/
    function execute($sql) {
        return mysql_query($sql,$this->conn);
    }
/***************************************************************************
     * 返回结构集中行数
     * return:number 数字
     **************************************************************************/
    function findcount($sql) {
        $result=$this->execute($sql);
        return mysql_num_rows($result);
    }
/***************************************************************************
     * 执行sql查询
     * return:array 数组
     **************************************************************************/
    function findbysql($sql) {
        $array=array();
        $result=mysql_query($sql);
        $i=0;
        while($row=mysql_fetch_assoc($result)) {
            $array[$i]=$row;
            $i++;
        }
        return $array;
    }
/***************************************************************************
    *$con的几种情况
    *空:返回全部记录
    *array:eg. array('id'=>'1') 返回id=1的记录
    *string :eg. 'id=1' 返回id=1的记录
    * return:json 格式数据
    ***************************************************************************/
    function toextjson($table,$start=0,$limit=10,$cons=) {
        $sql=$this->generatesql($table,$cons);
        $totalnum=$this->findcount($sql);
        $result=$this->findbysql($sql. limit .$start. ,.$limit);
        $resultnum = count($result);//当前结果数
        $str=;
        $str.= {;
        $str.= 'totalcount':' $totalnum',;
        $str.='rows':;
        $str.=[;
        for($i=0;$i            $str.={;
            $count=count($result[$i]);
            $j=1;
            foreach($result[$i] as $key=>$val) {
                if($j                    $str.='.$key.':'.$val.',;
                }
                elseif($j==$count) {
                    $str.='.$key.':'.$val.';
                }
                $j++;
            }
$str.=};
            if ($i != $resultnum-1) {
                $str.= , ;
            }
        }
        $str.=];
        $str.=};
        return  $str;
    }
/***************************************************************************
     * $table:表名
     * $cons:sql条件
     * return:sql语句
     **************************************************************************/
    function generatesql($table,$cons) {
        $sql=;//sql条件
        $sql=select * from .$table;
        if($cons!=) {
            if(is_array($cons)) {
                $k=0;
                foreach($cons as $key=>$val) {
                    if($k==0) {
                        $sql.=where ';
                        $sql.=$key;
                        $sql.='=';
                        $sql.=$val.';
                    }else {
                        $sql.=and ';
                        $sql.=$key;
                        $sql.='=';
                        $sql.=$val.';
                    }
                    $k++;
                }
            }else {
                $sql.= where .$cons;
            }
        }
        return $sql;
    }
/***************************************************************************
     * $table:表名
     * $cons:条件
     * return:xml格式文件
     **************************************************************************/
    function toextxml($table,$start=0,$limit=10,$cons=) {
        $sql=$this->generatesql($table,$cons);
        $totalnum=$this->findcount($sql);
        $result=$this->findbysql($sql. limit .$start. ,.$limit);
        $resultnum = count($result);//当前结果数
        header(content-type: text/xml);
        $xml='';
        $xml.=;
        $xml.=.$totalnum.;
        $xml.=;
        for($i=0;$i            $xml.=;
            foreach($result[$i] as $key=>$val)
                $xml.=.$val..$key.>;
            $xml.=;
        }
$xml.=;
        $xml.=;
        return $xml;
    }
/***************************************************************************
     * $table:表名
     * $mapping:数组格式头信息$map=array('no','name','email','age');
     * $filename:word文件名称
     * return:word格式文件
     **************************************************************************/
    function toword($table,$mapping,$filename) {
        header('content-type: application/doc');
        header('content-disposition: attachment; filename='.$filename.'.doc');
        echo '       xmlns:w=urn:schemas-microsoft-com:office:word 
       xmlns=[url=http://www.w3.org/tr/rec-html40]http://www.w3.org/tr/rec-html40[/url]>
'.$filename.'
';
        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'';
    }
/***************************************************************************
     * $table:表名
     * $mapping:数组格式头信息$map=array('no','name','email','age');
     * $filename:excel文件名称
     * return:excel格式文件
     **************************************************************************/
    function toexcel($table,$mapping,$filename) {
        header(content-type:application/vnd.ms-excel);
        header(content-disposition:filename=.$filename..xls);
        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代码:...
其它类似信息

推荐信息