I'm trying to export my MySQL query to MsExcel using PHP script, one execution of script I'm getting the following error:
Warning: mysql_field_name() expects parameter 1 to be resource, object given php file on line 161
The data is exporting, but headings (field names of the mysql of the table) are not exporting. Also, I expected to have every field on a separate cells, but its saved each row in one cell.
Here is the PHP Script:
$data = '';
$header = '';
$result = mysqli_query($GLOBALS['mysqli'], $export_sql) or die ("<b>Couldn't execute SQL query:</b> " . mysqli_error($GLOBALS['mysqli']));
$fields = mysqli_num_fields($result);
for ($i=0; $i < $fields; $i++) {
$header .= mysql_field_name($result, $i). "\t"; // line 161
}
while ($row = mysqli_fetch_row($result)) {
$line = '';
foreach ($row as $value) {
if ((!isset ($value)) || ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"'.$value.'"'."\t";
}
$line .= $value;
}
$data .= trim($line). "
";
}
$data = str_replace("", "", $data);
if ($data == "") {
$data = "
(0) Records Found!
";
}
header ("Content-type: application/octet-stream");
header ("Content-Disposition: attachment; filename=Export.xls");
header ("Pragma: no-cache");
header ("Expires: 0");
print "$header
$data";
Any suggestions to resolve the problem?