php怎样导出数据库为csv文件
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
php导入CSV文件:
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z这样就可以去掉表头的那一行
for ($i = 0; $i < count($data); $i++) {
$ziduan = $data[$i];
}
}
2.php导出CSV文件
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;
?>
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z这样就可以去掉表头的那一行
for ($i = 0; $i < count($data); $i++) {
$ziduan = $data[$i];
}
}
2.php导出CSV文件
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;
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询