User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,520 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,001 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2913 | Replies: 111 | Solved
Reply
Join Date: Feb 2008
Location: Trinidad
Posts: 408
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Problems with exporting from a mysql database to a .csv

  #1  
Jul 18th, 2008
Hello,

I am trying to do an export from a table within a mysql database into a csv file where my parameters are a start date and end date...but I am experiencing a few problems...

Background:
I have 2 files; testGCR.php and gcrFunction.php.

testGCR.php has my form where the user enters the relevant dates (I have a inserted calender to enter the dates...the date when chosen from the calender looks like "dd-mm-yyyy"), this is to be passed to the query...

gcrFunction.php has the validation of the data entered in the date boxes as well as the export code (which I got off the net )

Problem 1:
when the date is entered in the select query, only the column names are displayed with no data in the csv (even though the table has alot of data)

Problem 2:
in the csv file, just above the column names are the entire chunk of code from testGCR.php....it's not supposed to do that...it's only supposed to show me the results...

Any suggestions/comments are most welcome...

Thanks in advance
May
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 771
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #2  
Jul 18th, 2008
Just a tip to help you bypass the frustration:
Usually when I am exporting to csv, excel, pdf or anything, I always have two different modes, production mode and development mode.

In production mode, of course I am outputting the desired output like normal. In development mode I am just outputting straight to the browser or textarea or whatever allows me to analyze my output the easiest. I usually have this set in a variable at the top.

This allows you to analyze the actual behavior of your code without involving any more variables then necessary at the time.

Anyway, Post your code and I'll look at it.
Last edited by R0bb0b : Jul 18th, 2008 at 2:52 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 408
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #3  
Jul 18th, 2008
Hi, here are the 2 files...
Instead of having it output into the csv file, when i echoed it to the screen, it only shows the column headings...

testGCR.php
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Test Document</title>
  6. <script language="javascript" type="text/javascript" src="datetimepicker.js">
  7.  
  8. //Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
  9. //Script featured on JavaScript Kit (http://www.javascriptkit.com)
  10. //For this script, visit http://www.javascriptkit.com
  11.  
  12. </script>
  13.  
  14. </head>
  15.  
  16. <body>
  17. <?php
  18. include("./gcrFunction.php");
  19. ?>
  20. <form method="post">
  21. <table>
  22. <tr>
  23. <td width="747">
  24. <p align="left"><span class="style4 style25 style28 style6">Generate Chargeback Report</span></p>
  25. <p align="left"><span class="style21 style29 style5">The Chargeback Reporter works in 2 steps.</span></p>
  26. </td>
  27. </tr>
  28. <tr><td><p>&nbsp;</p></td></tr>
  29. <tr>
  30. <td>
  31. <table width="648" border="0" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFf">
  32. <tr>
  33. <td bgcolor="#99bebe" colspan="4">
  34. <p align="left" class="style8 style7 style32"><strong>
  35. <span class="style21 style33 style11">Step 1: Select Start and End Date</span></strong></p>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td width="100" bgcolor="#99bebe" scope="row">
  40. <div align="left" class="style8 style7 style31 style29 style13">
  41. <div align="right"><strong>Start Date: </strong></div>
  42. </div> </td>
  43. <td width="220" bgcolor="#99bebe">
  44. <input name="sDate" id="sDate" type="text" size="25">
  45. <a href="javascript:NewCal('sDate','ddmmyyyy')">
  46. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a Start Date">
  47. </a> </td>
  48. <td width="100" bgcolor="#99bebe" scope="row">
  49. <div align="left" class="style8 style7 style31 style29 style13">
  50. <div align="right"><strong>End Date: </strong></div>
  51. </div> </td>
  52. <td width="220" bgcolor="#99bebe">
  53. <input name="eDate" id="eDate" type="text" size="25">
  54. <a href="javascript:NewCal('eDate','ddmmyyyy')">
  55. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick an End Date">
  56. </a> </td>
  57. </tr>
  58. <tr>
  59. <td bgcolor="#99bebe" colspan="4">
  60. <p align="left" class="style5">
  61. <span class="style21 style30 style11">Step 2: Generate the Chargeback Report </span></p> </td>
  62. </tr>
  63. <tr>
  64. <td bgcolor="#99bebe"><p>&nbsp;</p></td>
  65. <td bgcolor="#99bebe" colspan="3">
  66. <div align="left">
  67. <strong>
  68. <input type="submit" value="Generate" name="btnSubmit" />
  69. </strong>
  70. </div>
  71. </td>
  72. </tr>
  73. </table>
  74. </td>
  75. </tr>
  76. </table>
  77. </form>
  78. <?
  79. validateData();
  80. ?>
  81.  
  82. </body>
  83. </html>

gcrFunction.php
  1. <?
  2. include('./connect.php');
  3.  
  4. error_reporting(E_ALL ^ E_NOTICE);
  5.  
  6. function validateData()
  7. {
  8. global $conn;
  9.  
  10. $sDate=$_POST['sDate'];
  11. $eDate=$_POST['eDate'];
  12. //echo $sDate;
  13. //echo $eDate;
  14.  
  15. $btn=$_POST['btnSubmit'];
  16.  
  17. if(isset($btn))
  18. {
  19. if(!$sDate)
  20. {
  21. echo '<center><font face=Verdana color=Red size="2"><b>Start Date field is empty!</b></font></center></td>
  22. </tr><tr bgcolor="#ffffff"><td>
  23. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  24. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  25. <td><p align="center" class="style30 style4" >&copy;ITT Department, Petrotrin. </p></td></tr></table>';
  26. die("Problem 1");
  27. }
  28. else if(!$eDate)
  29. {
  30. echo '<center><font face=Verdana color=Red size="2"><b>End Date field is empty!</b></font></center></td>
  31. </tr><tr bgcolor="#ffffff"><td>
  32. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  33. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  34. <td><p align="center" class="style30 style4" >&copy;ITT Department, Petrotrin. </p></td></tr></table>';
  35. die("Problem 2");
  36. }
  37. else
  38. {
  39.  
  40. function parseCSVComments($comments)
  41. {
  42. // First off escape all " and make them ""
  43. $comments = str_replace('"', '""', $comments);
  44. if(eregi(",", $comments) or eregi("\n", $comments))
  45. { // Check if I have any commas or new lines
  46. return '"'.$comments.'"'; // If I have new lines or commas escape them
  47. }
  48. else
  49. {
  50. return $comments; // If no new lines or commas just return the value
  51. }
  52. }
  53.  
  54. // Start our query of the database
  55. $sql = mysql_query("SELECT * FROM tsttbills where date between '$sDate' and '$eDate'", $conn) ;
  56. $numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching
  57.  
  58. if($numberFields)
  59. { // Check if we need to output anything
  60. for($i=0; $i<$numberFields; $i++)
  61. {
  62. // Create the headers for each column, this is the field name in the database
  63. $head[] = mysql_field_name($sql, $i);
  64. }
  65. $headers = join(',', $head)."\n"; // Make our first row in the CSV
  66.  
  67. while($info = mysql_fetch_object($sql))
  68. {
  69. foreach($head as $fieldName)
  70. { // Loop through the array of headers as we fetch the data
  71. $row[] = parseCSVComments($info->$fieldName);
  72. } // End loop
  73. $data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
  74. $row = ''; // Clear the contents of the $row variable to start a new row
  75. }
  76. // Start our output of the CSV
  77. $filename = $file."_".date("Y-m-d_H-i",time());
  78. header("Content-type: application/x-msdownload");
  79. header("Content-Disposition: attachment; filename=".$filename.".csv");
  80. header("Pragma: no-cache");
  81. header("Expires: 0");
  82. echo $headers.$data;
  83. }
  84. else
  85. {
  86. // Nothing needed to be output. Put an error message here or something.
  87. echo ("There is no data to create a CSV file.");
  88. }
  89. } //close the else
  90. }//close the if(isset($btn)) statement
  91. }
  92.  
  93.  
  94.  
  95. ?>
Last edited by maydhyam : Jul 18th, 2008 at 3:01 pm.
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 771
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #4  
Jul 18th, 2008
few slight changes, was getting headers already sent error: fixed
that's probably what was messing with the excel parsing.
that's about the only thing though, nice script.

liked the way you used
$head[] = mysql_field_name($sql, $i); gonna have to try that.


testGCR.php
  1. <?
  2. include("./gcrFunction.php");
  3. if(count($_POST) == 0)
  4. {
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. <title>Test Document</title>
  11. <script language="javascript" type="text/javascript" src="datetimepicker.js">
  12.  
  13. //Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
  14. //Script featured on JavaScript Kit (http://www.javascriptkit.com)
  15. //For this script, visit http://www.javascriptkit.com
  16.  
  17. </script>
  18.  
  19. </head>
  20.  
  21. <body>
  22.  
  23. <form method="post" target="_blank">
  24. <table>
  25. <tr>
  26. <td width="747">
  27. <p align="left"><span class="style4 style25 style28 style6">Generate Chargeback Report</span></p>
  28. <p align="left"><span class="style21 style29 style5">The Chargeback Reporter works in 2 steps.</span></p>
  29. </td>
  30. </tr>
  31. <tr><td><p>&nbsp;</p></td></tr>
  32. <tr>
  33. <td>
  34. <table width="648" border="0" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFf">
  35. <tr>
  36. <td bgcolor="#99bebe" colspan="4">
  37. <p align="left" class="style8 style7 style32"><strong>
  38. <span class="style21 style33 style11">Step 1: Select Start and End Date</span></strong></p>
  39. </td>
  40. </tr>
  41. <tr>
  42. <td width="100" bgcolor="#99bebe" scope="row">
  43. <div align="left" class="style8 style7 style31 style29 style13">
  44. <div align="right"><strong>Start Date: </strong></div>
  45. </div> </td>
  46. <td width="220" bgcolor="#99bebe">
  47. <input name="sDate" id="sDate" type="text" size="25">
  48. <a href="javascript<b></b>:NewCal('sDate','ddmmyyyy')">
  49. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a Start Date">
  50. </a> </td>
  51. <td width="100" bgcolor="#99bebe" scope="row">
  52. <div align="left" class="style8 style7 style31 style29 style13">
  53. <div align="right"><strong>End Date: </strong></div>
  54. </div> </td>
  55. <td width="220" bgcolor="#99bebe">
  56. <input name="eDate" id="eDate" type="text" size="25">
  57. <a href="javascript<b></b>:NewCal('eDate','ddmmyyyy')">
  58. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick an End Date">
  59. </a> </td>
  60. </tr>
  61. <tr>
  62. <td bgcolor="#99bebe" colspan="4">
  63. <p align="left" class="style5">
  64. <span class="style21 style30 style11">Step 2: Generate the Chargeback Report </span></p> </td>
  65. </tr>
  66. <tr>
  67. <td bgcolor="#99bebe"><p>&nbsp;</p></td>
  68. <td bgcolor="#99bebe" colspan="3">
  69. <div align="left">
  70. <strong>
  71. <input type="submit" value="Generate" name="btnSubmit" />
  72. </strong>
  73. </div>
  74. </td>
  75. </tr>
  76. </table>
  77. </td>
  78. </tr>
  79. </table>
  80. </form>
  81.  
  82. </body>
  83. </html>
  84. <?
  85. }
  86. validateData();
  87. ?>


gcrFunction.php
  1. <?
  2. include("inc/functions.inc");
  3. $conn = mysql_dbconn();
  4.  
  5. error_reporting(E_ALL ^ E_NOTICE);
  6.  
  7. function validateData()
  8. {
  9. global $conn;
  10.  
  11. $sDate=$_POST['sDate'];
  12. $eDate=$_POST['eDate'];
  13. //echo $sDate;
  14. //echo $eDate;
  15.  
  16. $btn=$_POST['btnSubmit'];
  17.  
  18. if(isset($btn))
  19. {
  20. if(!$sDate)
  21. {
  22. echo '<center><font face=Verdana color=Red size="2"><b>Start Date field is empty!</b></font></center></td>
  23. </tr><tr bgcolor="#ffffff"><td>
  24. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  25. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  26. <td><p align="center" class="style30 style4" >&copy;ITT Department, Petrotrin. </p></td></tr></table>';
  27. die("Problem 1");
  28. }
  29. else if(!$eDate)
  30. {
  31. echo '<center><font face=Verdana color=Red size="2"><b>End Date field is empty!</b></font></center></td>
  32. </tr><tr bgcolor="#ffffff"><td>
  33. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  34. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  35. <td><p align="center" class="style30 style4" >&copy;ITT Department, Petrotrin. </p></td></tr></table>';
  36. die("Problem 2");
  37. }
  38. else
  39. {
  40.  
  41. function parseCSVComments($comments)
  42. {
  43. // First off escape all " and make them ""
  44. $comments = str_replace('"', '""', $comments);
  45. if(eregi(",", $comments) or eregi("\n", $comments))
  46. { // Check if I have any commas or new lines
  47. return '"'.$comments.'"'; // If I have new lines or commas escape them
  48. }
  49. else
  50. {
  51. return $comments; // If no new lines or commas just return the value
  52. }
  53. }
  54.  
  55. // Start our query of the database
  56. $sql = mysql_query("SELECT * FROM tsttbills where date between '$sDate' and '$eDate'", $conn) ;
  57. $numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching
  58.  
  59. if($numberFields)
  60. { // Check if we need to output anything
  61. for($i=0; $i<$numberFields; $i++)
  62. {
  63. // Create the headers for each column, this is the field name in the database
  64. $head[] = mysql_field_name($sql, $i);
  65. }
  66. $headers = join(',', $head)."\n"; // Make our first row in the CSV
  67.  
  68. while($info = mysql_fetch_object($sql))
  69. {
  70. foreach($head as $fieldName)
  71. { // Loop through the array of headers as we fetch the data
  72. $row[] = parseCSVComments($info->$fieldName);
  73. } // End loop
  74. $data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
  75. $row = ''; // Clear the contents of the $row variable to start a new row
  76. }
  77. // Start our output of the CSV
  78. $filename = $file."_".date("Y-m-d_H-i",time());
  79. header("Content-type: application/x-msdownload");
  80. header("Content-Disposition: attachment; filename=".$filename.".csv");
  81. header("Pragma: no-cache");
  82. header("Expires: 0");
  83. echo $headers.$data;
  84. }
  85. else
  86. {
  87. // Nothing needed to be output. Put an error message here or something.
  88. echo ("There is no data to create a CSV file.");
  89. }
  90. } //close the else
  91. }//close the if(isset($btn)) statement
  92. }
  93.  
  94.  
  95.  
  96. ?>
Last edited by R0bb0b : Jul 18th, 2008 at 4:39 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 408
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #5  
Jul 21st, 2008
Hi,

When I applied the changes you did, I only get a blank page and not the page to select the dates...

liked the way you used
$head[] = mysql_field_name($sql, $i); gonna have to try that.
Thanks...

May
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 771
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #6  
Jul 21st, 2008
Oops, did you replace the top two lines in gcrFunction.php:
include("inc/functions.inc");
$conn = mysql_dbconn();

with your connection file
include('./connect.php');
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 408
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #7  
Jul 21st, 2008
Yea, I did and there's still a blank page...

from the file testGCR.php....with the following piece of code....isn't there supposed to be an open and closed square brackets?

  1. <?
  2. include("./gcrFunction.php");
  3. if(count($_POST[]) == 0)
  4. {
  5. ?>
Last edited by maydhyam : Jul 21st, 2008 at 1:06 pm.
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 771
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #8  
Jul 21st, 2008
move this line:
error_reporting(E_ALL ^ E_NOTICE);
on gcrFunction.php to just before the include statement on line 2 of testGCR.php
and comment out these lines.
  1. $filename = $file."_".date("Y-m-d_H-i",time());
  2. header("Content-type: application/x-msdownload");
  3. header("Content-Disposition: attachment; filename=".$filename.".csv");
  4. header("Pragma: no-cache");
  5. header("Expires: 0");
Last edited by R0bb0b : Jul 21st, 2008 at 1:09 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 771
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #9  
Jul 21st, 2008
Originally Posted by maydhyam View Post
Yea, I did and there's still a blank page...

from the file testGCR.php....with the following piece of code....isn't there supposed to be an open and closed square brackets?

  1. <?
  2. include("./gcrFunction.php");
  3. if(count($_POST[]) == 0)
  4. {
  5. ?>

I never include them. You could also try an if(isset($_POST['varname'])) statement
Last edited by R0bb0b : Jul 21st, 2008 at 1:09 pm.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 408
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #10  
Jul 21st, 2008
what is varname?
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.