Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no need to download external libraries - everything you need for this is already built into PHP.</p> <p>Steps:</p> <ol> <li>Do query in php to get the rows you want to output<br> You can use <code>SELECT * FROM table WHERE id IN (1,2,...)</code></li> <li>Use <code>mysql_fetch_array()</code> or <code>mysql_fetch_assoc()</code> to get the rows one at a time</li> <li>Use <code>fputcsv()</code> to output them to a file ending in csv - this will properly escape your data</li> </ol> <p><a href="http://www.php.net/manual/en/function.mysql-fetch-array.php" rel="noreferrer">http://www.php.net/manual/en/function.mysql-fetch-array.php</a><br> <a href="http://php.net/manual/en/function.fputcsv.php" rel="noreferrer">http://php.net/manual/en/function.fputcsv.php</a> </p> <p>Excel will be able to read the file. </p> <p>Override the defaults for <code>fputcsv</code> to use tabs for delimiters and Excel will have an even easier time reading the file. If you use commas (the default) you may need to pick commas as the delimiter on Excel import.</p> <p>Here's a working example assuming you already have rows set up:</p> <pre><code>$rows; // predefined $filename = 'webdata_' . date('Ymd') . '.csv'; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: application/octet-stream"); // that indicates it is binary so the OS won't mess with the filename // should work for all attachments, not just excel $out = fopen("php://output", 'w'); // write directly to php output, not to a file foreach($rows as $row) { fputcsv($out, $row); } fclose($out); </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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