Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 117,542 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,670 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Import/Export from MySQL database into Excel through PhP

 
Reply to this topicStart new topic

Import/Export from MySQL database into Excel through PhP

Maris Diana
post 10 May, 2008 - 06:39 AM
Post #1


New D.I.C Head

*
Joined: 10 May, 2008
Posts: 1



Thanked 1 times
My Contributions


Hello! I am a beginner in PhP and I would like to know how to import data from Excel files into a MySQL database by using PhP and also export data from the database into Excel or Pdf files. Need help fast! Thank you!
User is offlineProfile CardPM

Go to the top of the page


akozlik
post 15 May, 2008 - 03:25 PM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 527



Thanked 19 times

Dream Kudos: 750
My Contributions


QUOTE(Maris Diana @ 10 May, 2008 - 06:39 AM) *

Hello! I am a beginner in PhP and I would like to know how to import data from Excel files into a MySQL database by using PhP and also export data from the database into Excel or Pdf files. Need help fast! Thank you!


Hey,

The best way for you to do that would be to export the Excel file in a plain text delimited format. A good format to use would be tab delimited. This will place a tab between each item in the database. For instance:

John Doe 123 Main Street Anytown, USA

Which is actually the same as

John\tDoe\t123 Main Street\tAnytown, USA


You could then write a function that opens the text file generated, explodes() it on the tabs "\t", and loops through each row and inserts it into the database. I actually just did this at my job to help get a student database into Excel.

The reverse holds true when you're going the opposite way. You would want to loop through each row of your database, fwrite() them to a file, and separate each column by a tab.

CODE

$file = fopen("yourfile.txt", 'w');

$sql = "select * from users";
$result = mysql_query($sql) or die ( mysql_error() );

$fileString = ""

while ($row = mysql_fetch_array($result) ) {
     $fileString = $row['username'] . "\t" . $row['password'] . "\t" . $row['firstname'] . "\n";
}

fwrite($file, $fileString);
fclose($file);


That would loop through a table named users, and output each field into a row. There are more elegant ways to do it, but that should at least give you the general concept. You could then import that file as a tab delimited file in Excel.

Hope that helps! I know it was a mouthful.

Andrew
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 05:29PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month