LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php script that reads content from text file (https://www.linuxquestions.org/questions/programming-9/php-script-that-reads-content-from-text-file-656204/)

adrianno2 07-16-2008 03:25 PM

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

TB0ne 07-16-2008 03:39 PM

Quote:

Originally Posted by adrianno2 (Post 3216821)
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.

avatardeviva 07-16-2008 03:45 PM

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);

adrianno2 07-21-2008 01:14 AM

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?


All times are GMT -5. The time now is 08:50 AM.