hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : Generating CSV Files
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

Generating CSV Files

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 02-20-2005, 02:49 AM
AdWatcher-Boris AdWatcher-Boris is offline
Web Hosting Evangelist
 
Join Date: Sep 2003
Location: New York
Posts: 494

Generating CSV Files


Greetings,

I was curious - we have a database with various information that we would like to allow users to export as a CSV file.

However, I'm having difficulties finding information on the specifics and techniques of doing so. Anybody has any advice oe tips on how to generate CSV files with information from the DB?

Boris

Reply With Quote


Sponsored Links
  #2  
Old 02-20-2005, 05:50 PM
error404 error404 is offline
Web Hosting Master
 
Join Date: Dec 2004
Location: Canada
Posts: 1,076
It should be pretty trivial. Select the data into an array, and for each row in the array, join the columns with ',' and then print a newline.

In PHP, for example:
PHP Code:
$file fopen('foo.csv''w') or die('Unable to open file');
$rows mysql_query('SELECT * FROM foo');
while (
$r mysql_fetch_array($rows))
{
   
fwrite($fileimplode(','$r) . "\n");
}
mysql_free_result($rows);
fclose($file); 

Reply With Quote
  #3  
Old 02-20-2005, 06:00 PM
toweter toweter is offline
New Member
 
Join Date: Feb 2005
Posts: 4
but this is not working if there are commas in the database, is it?

i'm not further experienced in working with *.csv files, but if the syntax used by microsoft excel isn't wrong this should work:
PHP Code:
$file fopen('foo.csv''w') or die('Unable to open file');
$rows mysql_query('SELECT * FROM foo');
while (
$r mysql_fetch_array($rows))
{
   for(
$x 0$x count($r); $x++)
   {
      if (
eregi(","$r[$x]))
      {
         
$r[$x] = '"'.$r[$x].'"';
      }
   }
   
fwrite($fileimplode(','$r) . "\n");
}
mysql_free_result($rows);
fclose($file); 


Last edited by toweter; 02-20-2005 at 06:10 PM.
Reply With Quote
Sponsored Links
Reply

Related posts from TheWhir.com
Title Type Date Posted
Malwarebytes Launches Data Scan-and-Backup Service Web Hosting News 2013-05-07 14:51:03
Court Documents Show Megaupload Helped Feds Take Down File Sharing Service NinjaVideo Web Hosting News 2012-11-21 10:53:49
Pancake.io, DropPages Let Users Host Web Site Files on DropBox Blog 2011-12-08 17:03:11
Web Host Hosting Ireland Launches Online Backup Service CloudBox Web Hosting News 2011-09-27 20:00:52
Web Host Future Hosting Adds New Features to CDN Web Hosting News 2011-06-16 17:30:23


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?