Hi guys,
I would like to have a link on my webpage that when clicked opens a csv file of my websites database in excel.
I've found a script to do this (below) which works fine, except for the fact in the first row it just puts a line of numbers and not the field names given to each column in the original database.
Can anyone suggest how I can alter it to display the field names instead of the numbers?
Many thanks in advance.
<?php
require_once('../../Connections/connBCSG.php');
mysql_select_db($database_connBCSG, $connBCSG);
$query_excel = "SELECT * FROM regions";
$excel = mysql_query($query_excel, $connBCSG) or die(mysql_error());
$row_excel = mysql_fetch_assoc($excel);
$totalRows_excel = mysql_num_rows($excel);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=regional.xls");
header("Pragma: no-cache");
header("Expires: 0");
$tablename ="regions"; // DB Table Name
function display_db_table($tablename, $connBCSG)
{
$query_string = "select * from $tablename";
$result_id = mysql_query($query_string, $connBCSG);
$column_count = mysql_num_fields($result_id);
while ($row = mysql_fetch_row($result_id))
{
// The folowing if/then statement prints out the column names.
if(!$dokeys){
$keys = array_keys($row);
foreach($keys as $key){
$data .= $key . "\t";
}
$dokeys = true;
}
$data .= "\n";
for ($column_num = 0;$column_num < $column_count; $column_num++)
{
$row[$column_num] = str_replace("\n",$lbChar,$row[$column_num]);
$row[$column_num] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$row[$column_num]);
$row[$column_num] = str_replace("\\","",$row[$column_num]);
$data .= "$row[$column_num]\t";
}
}
echo $data;
}
display_db_table($tablename, $connBCSG);
mysql_free_result($excel);
?>