我有一个表名tblstuden。它包含ids studentname,rollnumberm和classname。我想以CSV格式下载此数据。怎么可能?
我可以通过CSV文件将数据上传到表格中。上传代码如下。它对我有用。但现在我想以CSV格式下载数据。
<?php
if(isset($_POST['submit']))
{
$row = 1;
$file1 = $_FILES['file']['tmp_name'];
$file = fopen($file1, "r");
while (($data = fgetcsv($file, 8000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
$data[$c];}
echo $data[0];
$name = $data[0];
}
fclose($file);
?>
答案 0 :(得分:1)
您可以使用此代码下载csv文件。
$getRecord = mysql_query("SELECT id, studentname, rollnumber, classname FROM tblstuden");
while ($row = mysql_fetch_array($getRecord )) {
$csvFile .= $row['id'].",". $row['studentname'].",". $row['rollnumber'].",".$row['classname']."\r\n";
}
header("Content-type: application/octet-stream");
header("Content-disposition:attachment; filename="."testCSV.csv");
echo $csvFile;