Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-16-2008, 03:25 PM
|
#1
|
|
Member
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39
Rep:
|
php script that reads content from text file
hi.
as subject says, is it possible to build a php script that reads a text file and add it to a mysql database?
example:
i have a file named contacts.txt, in this file, i have some names and phone numbers like this:
name1, phone#1
name2, phone#2
name3, phone#3
..............
..............
..............
and list continues, i have about 200 lines in this file.
i have basic knowledge about php, i know how to work with a database from php but i'm confused about reading text from files in php.
can you tell me what functions i need to use and explain me how can i build this if is possible?
thanks
|
|
|
|
07-16-2008, 03:39 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 12,003
|
Quote:
Originally Posted by adrianno2
hi.
as subject says, is it possible to build a php script that reads a text file and add it to a mysql database?
example:
i have a file named contacts.txt, in this file, i have some names and phone numbers like this:
name1, phone#1
name2, phone#2
name3, phone#3
..............
..............
..............
and list continues, i have about 200 lines in this file.
i have basic knowledge about php, i know how to work with a database from php but i'm confused about reading text from files in php.
can you tell me what functions i need to use and explain me how can i build this if is possible?
thanks
|
Very possible, and fairly easy. Check out http://www.php.net, that should get you a list of all the functions you're looking for. In particular, search for mysql_connect, and check out this example:
http://us.php.net/manual/en/mysql.examples.php
and
http://us.php.net/manual/en/function.fread.php
on how to read a file. This page
http://www.modwest.com/help/kb.phtml?qid=253&cat=6
Might give you a better idea.
|
|
|
|
07-16-2008, 03:45 PM
|
#3
|
|
Member
Registered: Jan 2004
Location: Almost Canada :-p
Posts: 34
Rep:
|
I'll throw my two cents in too:
Code:
<?php
$file='test.txt';
$content=file_get_contents($file); //Donno how much different this is
//than using fread, but it works!
$pieces = array();
$pieces = explode("\n",$content);
foreach($content as $line){
//Do something, like split it again!
$linedata = array();
$linedata = explode(",",$line);
print_r($linedata); //Lets see whats in there
}
?>
Now granted, I just wrote that offhand, but thats basically the idea. You can use the $linedata array to put things in where they are supposed to go, for example:
Code:
mysql_query("insert into somewhere (`col1`,`col2`,`col3`) values ('{$linedata[0]}','{$linedata[1]}','{$linedata[2]}')",$db);
|
|
|
|
07-21-2008, 01:14 AM
|
#4
|
|
Member
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39
Original Poster
Rep:
|
thanks TB0ne and avatardeviva, now i'm trying that
sorry for my late reply, i had a harddisk failure
LE:
@avatardeviva: your script throws me an error:
Warning: Invalid argument supplied for foreach() in /home/ra/www/index.php on line 9
line 9 is this:
foreach($content as $line){
whats the problem?
Last edited by adrianno2; 07-21-2008 at 03:17 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:35 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|