php原生态导出数据库文件
2017-02-10 15:58
330 查看
php不用外接类,导出sql数据:
<?php header('Content-type: text/html; charset=utf-8'); header("Content-type:application/vnd.ms-excel;charset=UTF-8"); header("Content-Disposition:filename=订单表.xls"); $conn = mysql_connect("localhost","root","123456") or die("不能连接数据库"); mysql_select_db("myfarm", $conn); mysql_query("set names 'UTF-8'"); $sql="select * from orderlist"; $result=mysql_query($sql); $title=array_keys(mysql_fetch_array($result,MYSQL_ASSOC)); echo $tit=join($title,"\t")."\n"; while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ echo join(array_values($row),"\t")."\n"; } ?>
第二种方法(PDO):
//输出列名字: $title=$pdo->query("show columns from orderlist"); while ($tit=$title->fetchColumn()) { $tit1.=$tit."\t"; }; echo $tit1."\n"; //输出订单内容: $sql="select * from orderlist"; $result=$pdo->query($sql); while($row=$result->fetch(PDO::FETCH_ASSOC)){ echo join(array_values($row),"\t")."\n"; }
写成类:
<?php session_start(); if (!$_SESSION['adminname']) { echo "<script>alert('您还没有登录,请先登录!');top.location='/$dirn/admin/login/login.php'</script>"; } function outFile($titlesql,$contentsql,$fileName){ header('Content-type: text/html; charset=utf-8'); header("Content-type:application/vnd.ms-excel;charset=UTF-8"); header("Content-Disposition:filename=$fileName.xls"); $dirn=dirname(__FILE__); include("$dirn/public/sql.php"); //输出列名字: $title=$pdo->query("$titlesql"); while ($tit=$title->fetchColumn()) { $allTit.=$tit."\t"; }; echo $allTit."\n"; //输出订单内容: $result=$pdo->query("$contentsql"); while($row=$result->fetch(PDO::FETCH_ASSOC)){ echo join(array_values($row),"\t")."\n"; } } $titlesql="show columns from user"; $contentsql="select * from user"; $fileName="用户表"; outFile($titlesql,$contentsql,$fileName); ?>
相关文章推荐
- PHP 实现从数据库导出到.csv文件方法
- php定时备份数据库,导出sql文件
- 在Yii框架中使用PHPExcel扩展从数据库导出excel文件功能的实现
- 用 PHP 导出 MySQL 数据库内容为 .sql 文件
- linux 数据库导入导出 php文件限制 数据库文件所在位置
- php 数据库中的回车换行导出excel文件时切换单元格的解决办法
- phpmyadmin导出数据库为什么是php文件
- asp.net从数据库导出数据到word、excel、txt文本文件
- php数据导入导出之excel(csv文件)
- asp.net2.0 利用Dataset,从数据库导出并生成无限级类别的XML文件
- 将数据库中的二进制数据导出为文件
- oracle数据库文件中的导入/导出(imp/exp命令)
- php文件格式及其导出
- PHP导出Excel文件
- php数据导入导出之excel(csv文件)
- oracle数据库文件中的导入\导出
- Oracle 使用数据泵 expdp impdp 导入导出数据库“表空间”文件
- asp.net从数据库导出数据到word、excel、txt文本文件
- oracle数据库文件中的导入/导出(imp/exp命令)详解
- 采用jxl实现数据库结果集导出到excel文件