-1

我正在尝试从 phpmyadmin 数据库下载“.csv”,但它正在下载一些空白文件。相同的查询非常适用于 phpmyadmin 中的 sql。下面是代码:

function get_csv()
{ 
 $filename="c:/mydata.csv";
 $var=mysql_query("SELECT * INTO OUTFILE $filename FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY \"\n\" FROM course");
 header("Content-type:  text/plain");
 header("Content-Disposition: attachment; filename=$filename.xls");
 header("Content-Transfer-Encoding: binary");
 header("Pragma: no-cache");
 header("Expires: 0");
 //print "$header\n$data";
 return 0;
}

在取消注释打印语句时,我得到一个显示一些错误的文件:

<br />
<b>Notice</b>:  Undefined variable: header in <b>C:\xampp\htdocs\SEN_PROJECT_Latest\SEN-PROJECT\class\database.php</b> on line <b>485</b><br />
<br />
<b>Notice</b>:  Undefined variable: data in <b>C:\xampp\htdocs\SEN_PROJECT_Latest\SEN-PROJECT\class\database.php</b> on line <b>485</b><br />
4

2 回答 2

0

我猜您尝试下载 CSV 文件,无需依赖此函数之外的其他变量,只需使用 readfile() 您应该会看到正在下载的文件。

function get_csv()
{ 
  $filename="c:\\mydata.csv";
  $var=mysql_query("SELECT * INTO OUTFILE $filename FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY \"\n\" FROM course") or die(mysql_error());
  header("Content-type:  text/plain");
  header("Content-Disposition: attachment; filename=$filename.xls");
  header("Content-Transfer-Encoding: binary");
  header("Pragma: no-cache");
  header("Expires: 0");
  ob_clean();
  flush();
  readfile($filename);
  return 0;
}

如果该代码未下载文件,您可以检查 c:/mydata.csv 并查看它是否已创建。

于 2013-04-07T07:32:07.310 回答
0

中的文件名INTO OUTFILE需要是字符串文字:

"SELECT * INTO OUTFILE '$filename' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY \"\n\" FROM course"
于 2013-04-07T09:00:37.987 回答