Jump to content


Photo

Mysql --> Excel


  • Please log in to reply
2 replies to this topic

#1 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 16 August 2006 - 02:28 PM

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
$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";  
?>

thanks for the help guys I really appreciate it!


#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 16 August 2006 - 04:26 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 16 August 2006 - 06:02 PM

cause boss said thats what he wants  :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users