from:http://www.xland.com.cn/article/7/81/0804/28778.htm
本类实现:
数据库信息导出: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,'档案');
//效果如下图
excel.JPG (27.04 KB)
2008-4-19 11:09
PHP代码:
以下是代码片段:
class Db
{
var $conn;
function Db( $host="localhost", $user="root", $pass="", $db="mytest")
{
if(! $this->conn=mysql_connect( $host, $user, $pass))
die("can't connect to mysql sever");
mysql_select_db( $db,&ensp