Note that there are some explanatory texts on larger screens.

plurals
  1. POfputcsv and fwrite not working
    primarykey
    data
    text
    <p>I have followed the explanation given <a href="http://www.coderelic.com/2012/01/export-data-from-a-database-to-csv-excel-with-php/" rel="nofollow">here</a> 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.</p> <p>Here is my code to first open the file:</p> <pre><code>&lt;?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'); ?&gt; </code></pre> <p>Then the query part is somewhat like this (I have multiple of such query parts each one calling the same function) :</p> <pre><code>&lt;?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 "&lt;tr&gt;&lt;td align='center' colspan=6&gt; $sterm not found! &lt;/td&gt; &lt;/tr&gt;";} elseif (mysql_num_rows($result)&gt;0) {result_disp($result);} } ?&gt; </code></pre> <p>then writing the result to file via a function is here:</p> <pre><code>&lt;?php function result_disp($results) { if($fp &amp;&amp; $results) { while ($rows = mysql_fetch_row($results)) { fputcsv($fp, array_values($rows),chr(9)); } die; } } </code></pre> <p>And finally closing the file at end of script</p> <pre><code>fclose($fp); ?&gt; </code></pre> <p>Thanks </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload