I have followed the explanation given here to write the query result to a file. But in this case I have to open and write the files headers first. Then keep writing/appending the query results one by one for multiple queries. This appending part I have written as a function. The problem that my script write the file with the headers (from the first part) only, it does not follow the fputcsv commands present in the function when it is called. Can you help me in solving this.
Here is my code to first open the file:
<?php
$fp = fopen('php://output', 'w');
$headers = array("Index","Gene_symbol","Gene_Name","Human_entrez","Rat_entrez","Mouse_entrez","DbTF","PMID");
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.txt"');
fputcsv($fp, $headers,chr(9));
header('Pragma: no-cache');
header('Expires: 0');
?>
Then the query part is somewhat like this (I have multiple of such query parts each one calling the same function) :
<?php
if (is_numeric($sterm))
{
$query="select * from tf where entrez_id_human=$sterm || entrez_id_rat=$sterm || entrez_id_mouse=$sterm";
$result=mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result)==0)
{echo "<tr><td align='center' colspan=6> $sterm not found! </td> </tr>";}
elseif (mysql_num_rows($result)>0)
{result_disp($result);}
}
?>
then writing the result to file via a function is here:
<?php
function result_disp($results)
{
if($fp && $results)
{
while ($rows = mysql_fetch_row($results))
{
fputcsv($fp, array_values($rows),chr(9));
} die;
}
}
And finally closing the file at end of script
fclose($fp);
?>
Thanks