2

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

Caramiriel
  • 7,029
  • 3
  • 30
  • 50
Konika Chawla
  • 141
  • 2
  • 5
  • $fp is not available inside of your function. Set your error_reporting to a sensible debugging value (E_ALL), then PHP will tell you stuff like this itself. And go read up on the basics of variable scope, http://www.php.net/manual/en/language.variables.scope.php – CBroe Jul 03 '13 at 12:16
  • Also note that the csv-headers are between the HTTP headers. They should be placed after the latter separated with a line-break. – Caramiriel Jul 03 '13 at 12:39
  • Thanks, I tried error_reporting(E_ALL) in beginning of code but it didn't report any error :(. Secondly on the page suggested for global variable I couldn't find if the filehandle $fp can be declared global. I tried global $fp both outside and within the function, but it didn't help either. Any suggestions how can I make it global. Thanks – Konika Chawla Jul 03 '13 at 13:10
  • Try adding `global $fp` before your first `if` statement in the function. – Bad Wolf Jul 03 '13 at 15:40

1 Answers1

0

Your first problem is that your file handle does not have scope within the function. The best way in my opinion is to pass it into the function:

....
     elseif (mysql_num_rows($result)>0)
     {result_disp($result, $fp);}
....

function result_disp($results, $fp)
{
if($fp && $results)
{
 while ($rows = mysql_fetch_row($results))
    {
        fputcsv($fp, array_values($rows),chr(9));
    } //DO NOT PUT "die()" HERE
 }
}

Your second problem is the "die()" statement inside the function. The purpose of "die()" is to stop the script entirely. It is PHP suicide. So, if you leave it in, your script will halt at the end of the first call of result_disp. That means not only would you never reach fclose($fp), you'll never reach any other call to result_disp.

Your third problem is that you are using mysql_* functions. These are deprecated (no longer in use) for several reasons. I have personal experience with database connection freezes caused by it. You should switch to mysqli or PDO.

miyasudokoro
  • 1,705
  • 1
  • 15
  • 23