Trying to export data from db to excel

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Trying to export data from db to excel

Post by farid »

Hi !!

Well I've been trying to export some data from a db but it keeps appering the following warning:

Warning: Cannot add header information - headers already sent by

My program is:

Code: Select all

@$link_id = mysql_connect("XXX", "XXX", "XXX") or die("Connection Failed");
$select_db = mysql_select_db("bd_amigokit",$link_id) or die("Connection Failed");

$select = "SELECT marca_modelo, promocion, tiempo_incluido, precio, costo_dolares, tipo_cambio, gain_loss, precio_iva, descuento_dist,
		costo_equipod, subsidio_ctotal, porcentaje_subsidio, margen_inversion, observaciones, fecha, circular FROM akLib_GSM WHERE estatus='Liberada-00' ORDER BY marca_modelo";                 
$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 "$headern$data";
Hoping someone could help me!! 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Thanks feyd!! 8)
Post Reply