Maison > Article > développement back-end > php使用header导出excel时会少第一行
http://192.168.0.200/test/khall.php?mindate=2014-04-01&maxdate=2014-04-30&keys=
<?php header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachement;filename=".$_GET['keys']."".date("Ymd").".xls"); echo iconv('utf-8', 'gbk', "时间\t类型\t区域\t姓名\t具体地址\t联系电话\t证件号\t套餐\t时长\t赠送\t金额\t经办人\t帐号\t备注\n"); include("db.php"); date_default_timezone_set("PRC"); $mindate=$_GET['mindate']; $maxdate=$_GET['maxdate']; $dates="dates>='".$mindate."' and dates<='".$maxdate."'"; if (!empty($_GET['keys'])) { $keys=$_GET['keys']; $ww="(`area` like '%".$keys."%' or `type` like '%".$keys."%' or `names` like '%".$keys."%' or `add` like '%".$keys."%' or `dur` like '%".$keys."%')"; $wh="where {$ww} and {$dates}"; } else{$wh="where {$dates}";} $sql="SELECT * FROM `khxx` {$wh}"; // echo "$sql"; mysql_query('set names gbk'); $query=mysql_query($sql); $rs=mysql_fetch_array($query); while ($rs=mysql_fetch_array($query)){ echo "{$rs['dates']}\t{$rs['type']}\t{$rs['area']}\t{$rs['names']}\t{$rs['add']}\t{$rs['phone']}\t{$rs['no']}\t{$rs['set']}\t{$rs['dur']}\t{$rs['give']}\t{$rs['moneys']}\t{$rs['attn']}\t{$rs['username']}\t{$rs['rem']}\n"; }?>
SELECT * FROM `khxx` where dates>='2014-04-01' and dates<='2014-04-30'
还有就是使用了mysql_query('set names gbk');
但是上个页面会传递$key变量值 因为传递来的值是utf-8的 在执行sql语句时就会失效
$rs=mysql_fetch_array($query); //你这里多读了一行
while ($rs=mysql_fetch_array($query)){
$keys=$_GET['keys'];
改为
$keys = iconv('utf-8', 'gbk', $_GET['keys']);
还有就是使用了mysql_query('set names gbk');
但是上个页面会传递$key变量值 因为传递来的值是utf-8的 在执行sql语句时就会失效
%".$keys."% 改为%". iconv('utf-8', 'gbk', '$keys')."%
$keys=$_GET['keys'];
改为
$keys = iconv('utf-8', 'gbk', $_GET['keys']);
今天少了你的提点可怎么办呀
认识了规律就好了