我正在尝试查询 mysql 数据库并在表格中显示数据。我现在想获取表格并制作一个按钮,允许您将其导出到 excel 文件。现在您可以导出到 excel,但是它显示一个 error Notice: Undefined variable: data
下面是我的代码:
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "export";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
$values =mysql_query( "SELECT name,email,phone,nationality,dob FROM users order by id");
$header = "Name" . "\t";
$header .= "Email" . "\t";
$header .= "Phone" . "\t";
$header .= "Nationality" . "\t";
$header .= "DOB" . "\t";
while( $row = mysql_fetch_assoc($values)){
$row1 = array();
$row1[] = $row['name'];
$row1[] = $row['email'];
$row1[] = $row['phone'];
$row1[] = $row['nationality'];
$row1[] = $row['dob'];
$data .= join("\t", $row1)."\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=expot.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
exit();
?>
最佳答案
因为您从未初始化 $data
变量。
放:
$data = '';
在代码的开头,while 循环之前。
关于php - 将mysql html表导出到excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757127/