LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-14-2003, 08:54 PM   #1
neon
Member
 
Registered: Aug 2003
Location: Thailand
Distribution: Fedora 3
Posts: 56

Rep: Reputation: 15
PHP & MySQL getting info from text file


Hi..
how do i get infor from a text file and insert it into a MySQL databse using PHP?

any ideas?

any help would be Absolutely Wonderful!
thanks
 
Old 10-15-2003, 12:34 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
Code:
<?php
mysql_connect("localhost","user_name") or die("could not connect");
mysql_select_db("my_database_name") or die("could not select database");
$file = "file_that_contains_names_one_name_in_a_line";
if (!$file_handle = fopen($file,"r")) { echo "Cannot open file."; }
if (!$file_contents = fread($file_handle, filesize($file))) { echo "Cannot read contents of file"; }
$array=preg_split('/\n/',$file_contents);
while(list($name,$value)=each($array))
{
        if($value!="")
        {
                $name_add_query="insert into name_table values('$value')";
                mysql_query($name_add_query);
        }
}
fclose($file_handle);
?>
I suppose that should do it. I don't have a linux machine right now available and so didn't check it .

But I think it works fine
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL PHP - text to display as url xtremedesign Linux - Newbie 3 04-01-2005 09:22 AM
LAMP(Linux,Apache,MySql,PHP) and other info hamtavs General 1 01-25-2005 03:54 AM
Getting info from text file alts Programming 16 11-19-2004 01:03 AM
php4 mysql, installation, php-pages with mysql info stay empty dnla Linux - Software 2 03-14-2004 02:54 PM
From RedHat9 to FreeBSD (Apache 2 & Mysql & PHP 4) guardian653 *BSD 5 12-11-2003 05:31 PM


All times are GMT -5. The time now is 07:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration