PCHomepage
Programmer
I am working on a small function to create CSV output from different mysqli queries and it is giving no errors itself but it seems to, for some strange reason, generate a JPGraph error. This function has nothing to do with JPGraph so did I inadvertently use a reserved word for that library?
Also, the resulting CSV seems to contain the HTML content of the page calling the function so clearly something is wrong.
As it is now it just prompts to save the file but I need it to provide a link for doing so.
Any ideas?
Also, the resulting CSV seems to contain the HTML content of the page calling the function so clearly something is wrong.
As it is now it just prompts to save the file but I need it to provide a link for doing so.
Any ideas?
Code:
function Data_2CSV($Query, $headerDisplayed = false, $mysqli) {
$fileName = 'DataDump.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
if ($result = $mysqli->query("$Query")):
while ($row = $result->fetch_array()):
if ( !$headerDisplayed ):
fputcsv($fh, array_keys($data));
$headerDisplayed = true;
endif;
fputcsv($fh, $data);
endwhile;
endif;
}