当前位置:编程学习 > XML/UML >>

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

数据库信息导出: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代码:
 
 
 
<?php
class Db {
    var $conn;
 
    /***************************************************************************
     * 连接数据库
     * return:MySQL 连接标识,失败返回FALSE
     **************************************************************************/
    function Db($host="localhost",$user="root",$pass="123456",$db="juren_gaokao") {
        if(!$this->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<$resultNum;$i++) {
            $str.="{";
            $count=count($result[$i]);
            $j=1;
            foreach($result[$i] as $key=>$val) {
                if($j<$count) {
                    $str.="'".$key."':'".$val."',";
            &nbs
补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,