justinsbabe5000
New member
I have the following code that selects data from a mysql database and imports into an excel file. It only retrieves the data. My question is how do I add the header for example: data is John Smith. How do I add header as fname lname? It should use the fieldnames of the table?
<?php
# Establish connection to database
mysql_connect('localhost', 'webuser', '') or DIE(mysql_error());
mysql_select_db('raceproject') or DIE("Unable to select database.");
// build query
$query= "select d.fname, d.lname, d.initial, g.gendername, r.racename, d.birthdate, s.year, s.district, s.state, t.statename, p.partyname, dc.votes, dc.win, ds.raisedamt, ds.spentamt
from seat s, candidate d, candcontest dc, candseat ds, race r, party p, gender g, states t
where d.candid = '{$_GET['candid']}'
and s.seatid = ds.seatid
and d.candid = dc.candid
and d.candid = ds.candid
and d.racecode = r.racecode
and dc.partycode = p.partycode
and d.gendercode = g.gendercode
and s.state = t.stateid";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$data[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}
$final = "<table>" . implode("\r\n", $data) . "</table>";
header("Content-type: application/vnd.ms-excel");
$fileName = 'raceproject.xls';
header("Content-Disposition: attachment; filename=$fileName");
echo $final;
?>
<?php
# Establish connection to database
mysql_connect('localhost', 'webuser', '') or DIE(mysql_error());
mysql_select_db('raceproject') or DIE("Unable to select database.");
// build query
$query= "select d.fname, d.lname, d.initial, g.gendername, r.racename, d.birthdate, s.year, s.district, s.state, t.statename, p.partyname, dc.votes, dc.win, ds.raisedamt, ds.spentamt
from seat s, candidate d, candcontest dc, candseat ds, race r, party p, gender g, states t
where d.candid = '{$_GET['candid']}'
and s.seatid = ds.seatid
and d.candid = dc.candid
and d.candid = ds.candid
and d.racecode = r.racecode
and dc.partycode = p.partycode
and d.gendercode = g.gendercode
and s.state = t.stateid";
$result = mysql_query($query);
$data = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$data[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}
$final = "<table>" . implode("\r\n", $data) . "</table>";
header("Content-type: application/vnd.ms-excel");
$fileName = 'raceproject.xls';
header("Content-Disposition: attachment; filename=$fileName");
echo $final;
?>