I can retrieve all the data from my table, but I want the title from first row on my table appear on excel too. Anyone can help me... TQVM <?php if (isset($_GET['evaluate'])) { $stud_id=$_GET['evaluate']; $db = mysql_connect("localhost","root") or die ("Could Not Connect to Server" . mysql_error()); /* select database */ mysql_select_db("eol", $db) or die ("Could Not Connect to Server" . mysql_error()); $query = "SELECT * FROM evaluate WHERE stud_id = '$stud_id' ORDER BY specimen_no"; $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; } ?> PHP:
You are fetching as only MYSQL_NUM; you're removign the table name so just dont do that part. or specify assoc. The default is both so you should just be able to do something like this - not tested, theres probably more efficient ways of doing so.... $count=0; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if($count == 0) $data = array_keys($row); else $data = array_values($row); $tsv[] = implode("\t", $data); $html[] = "<tr><td>" .implode("</td><td>", $data) . "</td></tr>"; } PHP:
Thanks lukeg32. You give me an idea. I test your coding, but the every row in excel appear a title of first row in mysql. So I change it little and I get a very close what I want but still can't reach my goal. The coding like this: <?php if (isset($_GET['evaluate'])) { $stud_id=$_GET['evaluate']; $db = mysql_connect("localhost","root") or die ("Could Not Connect to Server" . mysql_error()); /* select database */ mysql_select_db("eol", $db) or die ("Could Not Connect to Server" . mysql_error()); $flag = false; $query = "SELECT * FROM evaluate WHERE stud_id = '$stud_id' ORDER BY specimen_no"; $result = mysql_query($query) or die('Error, query failed'); $tsv = array(); $html = array(); while(false !== ($row = mysql_fetch_assoc($result))) { if(!$flag) { $data = array_keys($row); $flag = true; } else { $data = array_values($row); } $tsv[] = implode("\t", $data); $html[] = "<tr><td>" .implode("</td><td>", $data) . "</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; } ?> PHP: From this coding, the title appear but the data for first row not appear. It only appear for second data and so on. Do u have any idea... lukeg32
Oh sorry - I must off an increment at the end of the while loop..... this should be it! $count=0; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if($count == 0) $data = array_keys($row); else $data = array_values($row); $tsv[] = implode("\t", $data); $html[] = "<tr><td>" .implode("</td><td>", $data) . "</td></tr>"; $count++; } PHP: