473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Export MYSQL data for specific date into Excel/CSV via php

31
Hi,

I would like to export mysql data into excel/CSV for specific date.
For example i would like to generate data from 01-11-2012 to 31-12-2012, how do i perform this process?
I had done export mysql data into excel/CSV file for whole data in database but now the problem is i would like to generate data in date range. Can anyone help me. Thank You =)
Dec 3 '12 #1
3 5506
Rabbit
12,516 Expert Mod 8TB
Show us how you did it for a whole table.
Dec 3 '12 #2
huda89
31
I find this code from internet then i edit a little bit and apply it to my code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * PHP code to export MySQL data to CSV
  4.  * http://911-need-code-help.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html
  5.  *
  6.  * Sends the result of a MySQL query as a CSV file for download
  7.  */
  8.  
  9. /*
  10.  * establish database connection
  11.  */
  12.  
  13. $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
  14. mysql_select_db('SLA1', $conn) or die(mysql_error($conn));
  15.  
  16. /*
  17.  * execute sql query
  18.  */
  19.  
  20. $query = sprintf('SELECT * FROM report1');
  21. $result = mysql_query($query, $conn) or die(mysql_error($conn));
  22.  
  23. /*
  24.  * send response headers to the browser
  25.  * following headers instruct the browser to treat the data as a csv file called export.csv
  26.  */
  27.  
  28. header('Content-Type: text/csv');
  29. header('Content-Disposition: attachment;filename=export.csv');
  30.  
  31. /*
  32.  * output header row (if atleast one row exists)
  33.  */
  34.  
  35. $row = mysql_fetch_assoc($result);
  36. if ($row) {
  37.     echocsv(array_keys($row));
  38. }
  39.  
  40. /*
  41.  * output data rows (if atleast one row exists)
  42.  */
  43.  
  44. while ($row) {
  45.     echocsv($row);
  46.     $row = mysql_fetch_assoc($result);
  47. }
  48.  
  49. /*
  50.  * echo the input array as csv data maintaining consistency with most CSV implementations
  51.  * - uses double-quotes as enclosure when necessary
  52.  * - uses double double-quotes to escape double-quotes 
  53.  * - uses CRLF as a line separator
  54.  */
  55.  
  56. function echocsv($fields)
  57. {
  58.     $separator = '';
  59.     foreach ($fields as $field) {
  60.         if (preg_match('/\\r|\\n|,|"/', $field)) {
  61.             $field = '"' . str_replace('"', '""', $field) . '"';
  62.         }
  63.         echo $separator . $field;
  64.         $separator = ',';
  65.     }
  66.     echo "\r\n";
  67. }
  68. ?>
Dec 3 '12 #3
Rabbit
12,516 Expert Mod 8TB
You just need to change the sql query to filter for those dates.
Dec 3 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
2
by: Colin Graham | last post by:
I have a little problem i was wondering if anyone could help. I have created a table in Access and it has the following format. UniqueID FieldResult 100 1,3,5,7,23,7,8,9,12,4 103 ...
0
by: Roberto Lopes | last post by:
Hi, I have a dynamic Repeater, where I create some lines in run-time. I need to export this data to an Excel Spreadsheet. I used to do that from a datagrid, but the same solution doesn't work...
5
by: Dawid Pustulka | last post by:
How to export data to xls file using PHP? Is it possible to edit a look of document (like cell size, borders etc.) ? Please help.
0
by: jakeesgirl | last post by:
I have a search page in my program where users can search for member data. The results come up in the data grid view, but I also want users to be able to export the search results into an Excel...
2
by: jakeesgirl | last post by:
I have a search page in my program where users can search for member data. The results come up in the data grid view, but I also want users to be able to export the search results into an Excel...
1
by: milas57 | last post by:
hello everyone im new to php and mysql i been trying to export mysql data in an xml file my code below i have problem picking data in the database to display in the xml file, the xml file only...
2
by: Nosferatum | last post by:
Hi, is it possible to export mysql data to an excel spreadsheet? By PHP and with a structured, nice layout?
6
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in...
2
by: thesti | last post by:
hello, how to programmatically from VB .net / php. to import data from an EXCEL file? is there's no programmatic way, how to automated the process. as i search, the process is: Simply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.