After looking for a script to export mysql data into excel using PHP. I found one that was function.
Most things were incomplete or had others issues.
This one works, but it gives me each row into a column. I've been trying to swtich taht, so that I get rows. ( first row: column names, second row +: one record per row )
Could someone help me do that switch? I tried but it just broke the code. (Or if you have a complete, working code i could use )
Thanks a lot
Below is the code:
Most things were incomplete or had others issues.
This one works, but it gives me each row into a column. I've been trying to swtich taht, so that I get rows. ( first row: column names, second row +: one record per row )
Could someone help me do that switch? I tried but it just broke the code. (Or if you have a complete, working code i could use )
Thanks a lot
Below is the code:
PHP:
<?
include 'library/config.php';
include 'library/opendb.php';
$query = "SELECT fname, lname FROM students";
$result = mysql_query($query) or die('Error, query failed');
$tsv = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}
$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";
$fileName = 'mysql-to-excel.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
//echo $tsv;
echo $html;
include 'library/closedb.php';
?>