Ho provato prima lo script non funzionava. Per via delle variabili $header e $data
che non erano definite. Un errore che alcuni server non lo considerano nemmeno.
(Il mio invece si).
Ho aggiunto 2 semplici righe e va tutto a meraviglia! Grazie!
Codice PHP:
<?php
define ("DB_HOST", "localhost");
define ("DB_USER", "user");
define ("DB_PASSWORD", "pwd");
define ("DB_DATABASE", "db");
mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)
or die (mysql_error());
mysql_select_db (DB_DATABASE) or die (mysql_error());
# Qui dovresti aggiungere queste righe.
#
# if(isset($HTTP_GET_VARS['TextBox']) || isset($HTTP_POST_VARS['TextBox']) )
# {
# $TextBox = ( isset($HTTP_POST_VARS['TextBox']) )
# ? htmlspecialchars($HTTP_POST_VARS['TextBox'])
# : htmlspecialchars($HTTP_GET_VARS['TextBox']);
# }
# else
# {
# $TextBox = 'NULL';
# }
#
# E poi sostituire la Query SQL.
$select = "SELECT * FROM `tabella`";
$export = mysql_query ($select)
or die ("Sql error : " . mysql_error( ));
$fields = mysql_num_fields ($export);
$header = "";
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 ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data = "";
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>