exporting a .csv file from download button
<p>EDIT: I changed "echo($row)" to "print_r($row)" and now the csv that's being downloaded has each array in parenthesis with all the data mentioned, but how do I customize the way it all can be printed?</p>

<p>Right now, it's printing:</p>

<p>Array (
[id]=>1
[firstname]=>"Timmy"
)
Array (
....</p>

<p>(where each line is the next row in the excel spreadsheet)</p>

<p>How would I make this output so that each record has it's own row? for example the first column in excel would be id, then the second would be firstname, with each record it's own row?</p>

<p>I've got a link in my main.php:</p>

<pre><code>&lt;a class="btn" href="export.php"&gt;Export&lt;/a&gt;
</code></pre>

<p>Then here's my export.php file:</p>

<pre><code>&lt;?php

//open database connection
try {
    $db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8',
                    'DBUSER',
                    'DBPASS');

    $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
    $db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $ex) {
    echo "did not connect...";
}

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

$sql = "SELECT * FROM table_name;";

$sth = $db-&gt;prepare($sql);
$sth-&gt;execute();

$filename = date('d.m.Y').'.csv';

$data = fopen($filename, 'w');

while ($row = $sth-&gt;fetch(PDO::FETCH_ASSOC)) {
    fputcsv($data, $row);
    print_r($row);
}

echo "\r\n";

fclose($data);

?&gt;
</code></pre>