how to export a mysql data table search query into csv using php?
hear is query
if ($_REQUEST["startfrom"]<>'' and $_REQUEST["end"]<>'') {
$sql = "SELECT ECRNo,SalesDate,TimeSold,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,SUM(NetPrice) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate BETWEEN '".mysql_real_escape_string($_REQUEST["startfrom"])."'And'".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby;
} else if ($_REQUEST["startfrom"]<>'') {
$sql = "SELECT ECRNo,SalesDate,TimeSold,AmountDue,SalesDate,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,SUM(NetPrice) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate >= '".mysql_real_escape_string($_REQUEST["startfrom"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby;
} else if ($_REQUEST["end"]<>'') {
$sql = "SELECT ECRNo,SalesDate,TimeSold,SalesDate,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,SUM(NetPrice) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate <= '".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby;
}else {
$sql = "SELECT ECRNo,SalesDate,TimeSold,SalesDate,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,SUM(NetPrice) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE ECRNo>0 ".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby;
}
how do i export its query result?
All good answers here but if you want to do it using PHP, here is another option:
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
The $list array would be your results from the DB.
Taken from here
From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
SELECT *
FROM tableName
INTO OUTFILE '/tmp/fileName.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
This is work perfectly, Please try this...
SELECT * INTO OUTFILE "Database/business.sql"
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'
FROM `business`
Thank You.
you can use mysql feature:
SELECT * FROM tableName INTO OUTFILE 'filename.csv'
. I think it should work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.