If your problem is how to output tab separated data with column names
/*
SAMPLE DATA
+-------+-------+---------+------------+
| empid | fname | lname | dob |
+-------+-------+---------+------------+
| 1 | Peter | Smith | 1985-01-26 |
| 2 | Paul | Hartley | 1973-12-02 |
| 3 | Mary | Baker | 1980-04-11 |
| 4 | Jane | Doe | 1990-11-28 |
+-------+-------+---------+------------+ */
$res = $db->query("SELECT empid
, fname
, lname
, dob
FROM employee;
");
echo '<pre>';
$row = $res->fetch(PDO::FETCH_ASSOC);
echo join("\t", array_keys($row)) . "\n"; // headings
do {
echo join("\t", $row) . "\n"; // data
} while ($row = $res->fetch());
echo '</pre>';
Which gives
Similarly, if you want to write it to a csv file for export to Excel, then
$res = $db->query("SELECT empid
, fname
, lname
, dob
FROM employee;
");
$fp = fopen('AAA.csv', 'w');
$row = $res->fetch(PDO::FETCH_ASSOC);
fputcsv($fp, array_keys($row), "\t"); // headings
do {
fputcsv($fp, $row, "\t"); // data
} while ($row = $res->fetch());