Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP not creating downloadable CSV file
    primarykey
    data
    text
    <p>I am trying to pull out data from my database using php and exporting it into a downloadable CSV file that can be opened with excel. I am able to do this when i use mysql however, many have advised to not include mysql syntax in my code as its being deprecated and instead i should use mysqli. I have changed my code but now my code is not working. Does anyone know why that is?</p> <p><strong>mysql version (working version)</strong>`</p> <pre><code>mysql_connect('localhost', 'xxxxx', 'xxxxx') or die('connect'); mysql_select_db('db') or die('select'); $result = mysql_query('SELECT * bodyshops_master_network') or die('query'); if(mysql_num_rows($result) == 0) { die('no data'); } $fh = tmpfile() or die('tmpfile'); $cols = array_keys(mysql_fetch_assoc($result)); fputcsv($fh, $cols); mysql_data_seek($result, 0); // set result row pointer back to first row while($row = mysql_fetch_assoc($result)) { fputcsv($fh, $row); } rewind($fh); $text = fread($fh, 999999); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="download.csv"'); header('Content-Length: ' . strlen($text)); echo $text; exit; </code></pre> <p><strong>mysqli version (not working):</strong></p> <pre><code>$mysqli = new mysqli("localhost", "xxxxx", "xxxxx", "db"); if (mysqli_connect_errno()) { printf("Connect failed: ", mysqli_connect_error()); exit(); } else { $result = "SELECT * FROM bodyshops_master_network"; if(mysqli_num_rows($result) == 0) { die('no data'); } $fh = tmpfile() or die('tmpfile'); $cols = array_keys($result-&gt;fetch_assoc()); fputcsv($fh, $cols); $result-&gt;data_seek(0); // set result row pointer back to first row while($row = $result-&gt;fetch_assoc()) { fputcsv($fh, $row); } rewind($fh); $text = fread($fh, 999999); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="download.csv"'); header('Content-Length: ' . strlen($text)); echo $text; exit; </code></pre>
    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.
    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