Reputation: 1845
I am trying to query a mysql database and display data in a table.I now want to take the table and make a button that allows you to export it to an excel file.now you were able to export to excel,but its showing an error Notice: Undefined variable: data
Below is my code:
<?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();
?>
Upvotes: 1
Views: 1203
Reputation: 9398
Because you never initialize the $data
variable.
Put:
$data = '';
at the beginning of your code, before the while cycle.
Upvotes: 3
Reputation: 4774
Your code contains the statement:
$data .= join("\t", $row1)."\n";
This is where you're concatenating a string to an existing variable $data
. However, no such variable exists. You should add somewhere near the top of your code:
$data = "";
to initialize the $data
variable.
Upvotes: 2