Jump to content

Mysql --> Excel


Evanthes

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.
[code=php:0]
include('include.php'); // includes mysql  connection
// creating variable names
$searchterm=$HTTP_GET_VARS['searchterm'];
// 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"; 
?>
[/code]

thanks for the help guys I really appreciate it!
Link to comment
https://forums.phpfreaks.com/topic/17741-mysql-excel/
Share on other sites

Archived

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

×
×
  • Create New...

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.