hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : Export information from a database to a CSV spreadsheet with PHP
Reply

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

Export information from a database to a CSV spreadsheet with PHP

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-10-2012, 01:57 PM
j.vihavainen j.vihavainen is offline
Newbie
 
Join Date: Mar 2004
Posts: 13
*

Export information from a database to a CSV spreadsheet with PHP


Let’s assume that you already have a database setup with a table called “users” and you want to export users and their email addresses to a CSV file.

PHP Code:
<?php
 
// Connect and query the database for the users
$conn = new PDO("mysql:host=localhost;dbname=mydatabase"'myuser''mypassword');
$sql "SELECT username, email FROM users ORDER BY username";
$results $conn->query($sql);
 
// Pick a filename and destination directory for the file
// Remember that the folder where you want to write the file has to be writable
$filename "/tmp/db_user_export_".time().".csv";
 
// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle fopen($filename'w+');
 
// Write the spreadsheet column titles / labels
fputcsv($handle, array('Username','Email'));
 
// Write all the user records to the spreadsheet
foreach($results as $row)
{
    
fputcsv($handle, array($row['username'], $row['email']));
}
 
// Finish writing the file
fclose($handle);
 
?>
Notes:

Reply With Quote


Sponsored Links
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Webmail Messages to CSV? vessio Hosting Software and Control Panels 0 09-12-2011 09:57 AM
Export domain names to csv oozypal Domain Names 3 07-21-2009 05:44 PM
PHP CSV with different titles from same CSV file horizon Programming Discussion 4 06-22-2009 10:58 PM
Export a text file to CSV format. steveks Programming Discussion 10 11-05-2008 10:03 AM
Saving an Excel spreadsheet as a CSV file. Ron Web Hosting Lounge 1 02-23-2004 03:30 AM

Related posts from TheWhir.com
Title Type Date Posted
Phoenix NAP Responds to Customer Feedback with New Cloud Features Web Hosting News 2013-02-19 17:09:09
Samsung Ventures Backs Cloudant Database as a Service Web Hosting News 2013-02-08 14:10:26
Cloud Host Savvis Extends Cloud-Based Database Platform to European Customers Web Hosting News 2012-10-12 11:06:37
So you want to do business with Europe? Blog 2012-08-24 17:20:01
VMware vFabric Data Director 2.0 Adds Support for Oracle Databases Web Hosting News 2012-07-10 16:30:32


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?