Hello everyone,
I am trying to export mysql table to excel and I have the following code. Unfortunately it gives me an error message and I am not sure how to fix it.

elseif($_POST['excel']== "Export to Excel")
	{
		define(db_user, "excel");
		define(db_pass, "spreadsheet");
		$query = "SELECT * FROM personalinfo";                
$result = mysqli_query($cxn, $query); $fields = mysqli_num_fields($result); for ($i = 0; $i < $fields; $i++) { $header .= mysqli_field_name($result, $i) . "\t";} while($row = mysqli_fetch_row($result)) { $line = ''; foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); if ($data == "") { $data = "\n(0) Records Found!\n";
}
header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=Report.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$data";
}

And the error message is

Fatal error : Call to undefined function mysqli_field_name() in /usr/local/httpd/htdocs/retrieve.php on line 458

Thank you

    its saying it cannot find a funciton called mysqli_field_name

    there is a mysql_field_name in php.net, but no mysqli_field_name, check www.php.net and confirm you are using the appropriate funciton

      Ok I changed the mysqli_field_name($result, $i) to mysqlI_fetch_field($result) and now I get a new error on the same line

      Catchable fatal error : Object of class stdClass could not be converted to string in /usr/local/httpd/htdocs/retrieve.php on line 459

      and when I change it to mysqlI_fetch_field($result, $i) I can open the excel file but I get this error message

      Warning:mysqli_fetch_field() expects exactly 1 parameter, 2 given in usr/local/httpd/htdocs/retrieve.php on line 459

        7 days later

        I am still stuck on this and I would really really appreciate some help. The error I keep getting is
        Catchable fatal error : Object of class stdClass could not be converted to string in /usr/local/httpd/htdocs/retrieve.php on line 459

        for the line mysqli_fetch_fields($result). I think it might have to do with mysqli but I don't know how to go about fixing it.

        Thank you

          Write a Reply...