dongli8979 2013-04-12 13:38
浏览 59
已采纳

将数据导出到csv后出现未定义的变量错误[关闭]

i try to learn how to export data to csv using this script, yes i can export all data but in csv i found one error(when run in localhost) :

<b>Notice</b>:  Undefined variable: csv_output in <b>C:\xampp\htdocs\import_export\index.php</b> on line <b>10</b><br />

not sure why, but i can run this code in live server without any issue.

Full code

error_reporting(E_ALL);

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("csv") or die(mysql_error());

$file  = "product_export"; // csv name.

//CSV Header
$csv_output .= "ID " . ", ";
$csv_output .= "Content " . ", ";

$csv_output .= "
";

//CSV Content rows
$query = mysql_query("SELECT * FROM `users`");
while ($row = mysql_fetch_array($query)) {
    $csv_output .= $row["id"] . ", ";
    $csv_output .= $row["content"] . ", ";

    $csv_output .= "
";
}

$filename = $file . "_" . date("d-m-Y_H-i");

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=" . $filename . ".csv");

print $csv_output;

exit;

p/s : im using mysql_* for testing only.

  • 写回答

3条回答 默认 最新

  • dseomy1964 2013-04-12 13:40
    关注

    PHP is just letting you know that you are using a variable that hasn't been defined yet. It's a notice, not a warning or error.

    This line is prompting the notice:

    $csv_output .= "ID " . ", "; 
    

    It's the first use of this variable, you don't need concatenation at this point. Easy fix would be to change it to:

    $csv_output = "ID " . ", ";
    

    If you don't want PHP spitting out notices, you could change the first line of your script to:

    error_reporting(E_ALL ^ E_NOTICE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services