Jump to content


This topic is now archived and is closed to further replies.


Mysql --> Excel

Recommended Posts

I am running a program that queries the database, based on user input. Now i want to be able to backup specific searches to a excel file. All the Mysql queries are working dandy, and Ive followed the tutorial on the website to export to Excel but i must be missing somehting because everything (the columns and the data) is just printing into one cell. So I know I'm close to getting this to work seeing how my data is getting passed to the file but i guess my problem lies with the formatting. Does anyone know what the problem might be? Could it be something with my database? I'll post the code from the tutorial for reference.
include('include.php'); // includes mysql  connection
// creating variable names
// begin here with code to push data to PDF/EXCEL

$select = "select * from data.tbl_sites where zone = '".$searchterm."'" ;
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "\t";

while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {                                           
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        $line .= $value;
    $data .= trim($line)."\n";
$data = str_replace("\r","",$data);
if ($data == "") {
    $data = "\n(0) Records Found!\n";                       
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; 

thanks for the help guys I really appreciate it!

Share this post

Link to post
Share on other sites
why not database it, or flat file it in a txt file, outside the server.  I see people using excel but don't understand hte significance.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.