QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 433|回复: 1

[PHP开发] 请教一个问题?

[复制链接]

1

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2017-4-28 09:31:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
<?php
/*
* PHP code to export MySQL data to CSV
*
* Sends the result of a MySQL query as a CSV file for download
* Easy to convert to UTF-8.
*/

/*
* establish dataBase connection
*/

$conn = mysql_connect('localhost', 'root', 'porsche911') or die(mysql_error());
mysql_select_db('mysql', $conn) or die(mysql_error($conn));
mysql_query("SET NAMES UTF-8");
/*
* execute sql query   
*/
$query = sprintf("SELECT listid,workdate,worker.w_name,sub_info,p_name,quantity,p_price,this_price,subtotal,subtotal2 FROM worker,p_info,p_sub,worklist where PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( workdate, '%Y%m' ) ) =1 AND worker.w_id=worklist.w_id
and worklist.p_id=p_info.p_id
and p_info.sub_id=p_sub.sub_id order by w_name; ");
$result = mysql_query($query, $conn) or die(mysql_error($conn));
/*
* send response headers to the browser
* following headers instruct the browser to treat the data as a csv file called export.csv
*/
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment;filename=shangyue.csv');
/*
* output header row (if atleast one row exists)
*/

$row = mysql_fetch_assoc($result);
if ($row) {
  echocsv(array_keys($row));
}

/*
* output data rows (if atleast one row exists)
*/
while ($row) {
  echocsv($row);
  $row = mysql_fetch_assoc($result);
}

/*
* echo the input array as csv data maintaining consistency with most CSV implementations
* - uses double-quotes as enclosure when necessary
* - uses double double-quotes to escape double-quotes
* - uses CRLF as a line separator
*/

function echocsv($fields)
{
  $separator = '';
  foreach ($fields as $field) {
    if (preg_match('/\\r|\\n|,|"/', $field)) {
$field = '"' . str_replace('"', '""', $field) . '"';
    }
    echo $separator . $field;
    $separator = ',';
       
  }
  echo "\r\n";

}
?>
以上代码有什么问题吗?我这个代码是导出.csv文件,但是老是报错Warning: sprintf(): Too few arguments in C:\AppServ\www\jayi v1.0\phpcsv_shangyue(ce).php on line 21
Query was empty,这是怎么回事儿,数据库语句在mysql里面运行没有问题,放到PHP里面就报错!!

4

主题

21

帖子

174

积分

新手上路

Rank: 1

积分
174
QQ
发表于 2017-7-21 15:04:36 | 显示全部楼层
提示很清楚了,sprintf的参数少了
write code every day
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

     
    战略合作|联系方式|广告赞助|商务合作|手机版|小黑屋|( 京ICP备14036609号-6 )

GMT+8, 2017-8-24 16:38

© 2013-2017 Powered by Discuz! X3.3. 本站由 又拍云 提供 CDN 图片存储服务

快速回复 返回顶部 返回列表