我有一个简单的过程来创建一个包含数据库数据的CSV文件。
if (isset($_POST['submitcsv'])) {
$csvfile = fopen("stardata.csv", "w"); //w=write, r=read, a=append
$datebeginning = date('Y/m/d', strtotime($_POST['datebeginning']));
$query = $pdoace->prepare("SELECT *
FROM `starrisk`
WHERE `testdate` >= ?
AND `site` = '". $_POST['fromlocation'] ."'
ORDER BY `cdcnumber` ASC");
$query->execute(array($datebeginning));
While($row = $query->fetch()) {
for ($x=1; $x<=51; $x++)
{
echo fwrite($csvfile, $row[$x].",");//inside loop;
}
echo fwrite($csvfile, "\r\n");
}
fclose ($csvfile);
echo 'Data Compilation Complete: <a href="stardata.csv">download now...</a>';
}
我得到的神秘数字看起来只是数据编译完成(下面),似乎与数据本身无关(它由一个名字,姓氏和一堆-1和1&#39;组成。 s(真假))......
117342222333222233223232233332232232332223323233333332117672223232323222233322222233233232323233323233322222数据编译完成:立即下载...
任何人的想法???
答案 0 :(得分:5)
echo
之前的fwrite
将输出写入的字节数。参考:http://php.net/fwrite#refsect1-function.fwrite-returnvalues
答案 1 :(得分:1)
将代码更改为:
while($row = $query->fetch()) {
fputcsv ( $csvfile , $row );
}