谁能教我用php把mysql数据库中的信息导出到csv文件中 10
1个回答
展开全部
1.php导出CSV文件
<?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;
<?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;
追问
你有qq吗 我家你qq吧 这上面说不清
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |