export to text file
Posted: Sat Feb 25, 2006 10:45 am
I have a script to export a mysql table to a text file.
It works except I noticed that it is not adding the first row from the table?
So if I have only 1 row the text file will contain no data.
If I have 2 rows or more, it will leave off whichever record is first in the table.
I added a line to test to see how many rows I was receiving and I get the correct number, but still doesn't display the first row from the table?
So, is it something with the way I am adding to the rows to the $csv_output?
It works except I noticed that it is not adding the first row from the table?
So if I have only 1 row the text file will contain no data.
If I have 2 rows or more, it will leave off whichever record is first in the table.
I added a line to test to see how many rows I was receiving and I get the correct number, but still doesn't display the first row from the table?
So, is it something with the way I am adding to the rows to the $csv_output?
Code: Select all
//// EXPORT to file
$sql = "SELECT * from `vehicles`
WHERE `vehicle_active` = '1'
ORDER by `inv_type_function` ASC, `inv_type_name` ASC, `year` DESC, `price` DESC";
$result = mysql_query($sql);
$csv_output = strtoupper($get_dealer_qry['dealer']);
$csv_output .= "\r\n";
$csv_output .= "Visit us online ".$get_dealer_qry['domain'];
$csv_output .= "\r\n";
//// Column headings
$csv_output .= "YEAR"."\t"."MAKE"."\t"."MODEL"."\t"."MILEAGE"."\t"."ENGINE"."\t"."DRIVE"."\t"."TRANSMISSION"."\t".
"EXTERIOR COLOR"."\t"."PRICE"."\t"."FEATURE1"."\t"."FEATURE2"."\t"."FEATURE3"."\t"."FEATURE4"."\t"."FEATURE5";
$csv_output .= "\r\n";
//// test-remove
$csv_output .= mysql_num_rows($result)."\r\n";
/////
$get_qry = mysql_fetch_array($result);
if (mysql_num_rows($result)) {
$get_qry['price'] = number_format($get_qry['price']);
$get_qry['mileage'] = number_format($get_qry['mileage']);
while($get_qry = mysql_fetch_array($result)) {
if ($get_qry['inv_type_function'] == 1) {
$csv_output .= $get_qry['year']. "\t" .$get_qry['make']. "\t" .$get_qry['model']. "\t" .$get_qry['mileage']. "\t" .
$get_qry['engine']. "\t" .$get_qry['drive']. "\t" .$get_qry['transmission']. "\t" .$get_qry['exterior']. "\t" .
"$".$get_qry['price']. "\t" .$get_qry['feature1']. "\t" .$get_qry['feature2']. "\t" .$get_qry['feature3']. "\t" .
$get_qry['feature4']. "\t" .$get_qry['feature5']. "\r\n";
}
else {
$csv_output .= "\t" .$get_qry['make']. "\t" . "\t" . "\t" . "\t" . "\t" . "\t" . "\t" ."$".$get_qry['price']. "\t" .
$get_qry['feature1']. "\t" .$get_qry['feature2']. "\t" .$get_qry['feature3']. "\t" .$get_qry['feature4']. "\t" .
$get_qry['feature5']. "\r\n";
}
}
}
$csv_output = trim($csv_output);
header ("Content-type: text");
header ("Content-Disposition: attachment; filename=export_" . date("m-d-Y") . ".txt");
print $csv_output;
exit;