【发布时间】:2013-04-16 10:36:05
【问题描述】:
我有这段代码可以将 MySQL 数据导出到 CSV 文件,但是当我回显结果时它只显示数据库中的 1 行,即使我在 PHP My Admin 中运行 SQL,它也会显示大约 29 行。
它只是生成一个空白的 CSV 文件
$sql="select description, jobreceived, timebookedfor, bookedfor,
site_contact, site_address, invoice_contact, invoice_address,
quotedprice, cleardowndetails, notes, mo_number from jobs ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$filename="jobs.csv";
$file=fopen($filename,"w");
//$output="sequence,firstname,surname,email,membertype\n";
fwrite($file,$output);
while($result=mysql_fetch_array($rs))
{
echo $result["description"].','.$result["jobreceived"].'<br>';
//$output=$result["sequence"].",".$result["name"].","
.$result["email"].",".$result["country"]."\n";
$output=proper($result["description"]).",".$result["jobreceived"]
."\r\n";
fwrite($file,$output);
}
fclose($file);
function proper($string)
{
$first=strtoupper(substr($string,0,1));
$rest=strtolower(substr($string,1));
$result=$first.$rest;
return $result;
}
【问题讨论】:
-
开始使用 PHP 内置的 fputcsv() 函数 (php.net/manual/en/function.fputcsv.php)
-
错误日志是怎么说的?
-
什么错误日志?我尝试使用 fputcsv 而不是 fopen 但仍然没有
-
您对目录和jobs.csv 文件有写权限吗? (你说它是空白的,所以我假设它已经存在)你应该通过
if(!$file) echo 'cannot open jobs.csv for writing';进行检查