日期:2014-05-17  浏览次数:20458 次

php 怎么导出数据?
php 查询出来的数据怎么导出来生成sql文件?比如:查询A得到的所有属于A的数据,然后就只要导出属于A的所有数据,生成sql文件。

------解决方案--------------------
参考 http://jianzhong5137.blog.163.com/blog/static/982904920109115394478/
------解决方案--------------------
<?php
header( "Cache-Control: public" );
    header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=txxx.csv");
    header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str =  iconv("utf-8",'gbk',"信息id,标题,名称,电话,QQ,Email,内容,时间\n");
$file_str="";

$mysqli= new mysqli('localhost','root','','test');

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$sql='select *  from messages';
$mysqli->query("set names utf8 ;");
$result=$mysqli->query($sql);

if($result){
while ($row = mysqli_fetch_assoc($result)){

$file_str.= $row['id'].','.$row['title'].','.$row['name'].','."'{$row['telephone']}'".','.$row['qq'].','.$row['email'].','.str_ireplace(',',',',$row['content']).','.$row['retime']."\n";
}
}else{
echo "nonono!!!";
}
$file_str=  iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;

?>