# Thread: How to transfer data to mysql from .txt file

1. ## How to transfer data to mysql from .txt file

Hi all i got a txt file that has a list of urls i want to transfer this to a table in mysql but i do not know how. I have phpmyadmin and mysql installed . Could an expert show me an easy and fast way to store these urls in a database.
The text file hast the urls in this format :

http://www.localhost.com/file1.rm
http://www.localhost.com/file2.rm
http://www.localhost.com/file3.rm
http://www.localhost.com/file4.rm
.......................
.......................

Thanks

2. ## Re: How to transfer data to mysql from .txt file

This should give you an idea....

PHP Code:
 $fc=file("yourfile.txt"); //loop through file foreach($fc as $LineInfo) { //Add your code for inserting a record into your database here }  3. ## Re: How to transfer data to mysql from .txt file Originally Posted by lintz This should give you an idea.... PHP Code: $fc=file("yourfile.txt"); //loop through file foreach($fc as$LineInfo) { //Add your code for inserting a record into your database here }  
Thank u for u reply. could u tell me how to refere to each line url in mysql statment? insert into tablename values ( ????)

4. ## Re: How to transfer data to mysql from .txt file

Originally Posted by tony007
Thank u for u reply. could u tell me how to refere to each line url in mysql statment? insert into tablename values ( ????)

PHP Code:
 <?php   // Presuming you have connected to the DB already...   $Contents = file('yourfile.txt'); foreach($Contents as $Line) {$Query = mysql_query("INSERT INTO tblName (ID, URL) VALUES('', '" . $Line . "');"); if (mysql_error($Query)) {       echo mysql_error($Query); } }  Cheers, Ryan Jones 5. ## Re: How to transfer data to mysql from .txt file Originally Posted by sciguyryan Something like this (pesuadocode): PHP Code:  <?php // Presuming you have connected to the DB already...$Contents = file('yourfile.txt');   foreach($Contents as$Line) {     $Query = mysql_query("INSERT INTO tblName (ID, URL) VALUES('', '" .$Line . "');");     if (mysql_error($Query)) { echo mysql_error($Query);     }   } 

Ryan Jones
Thank u for u reply. I keep getting the following error :

VB Code:
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in \text2mysql.php on line 29

an pointing at this line :

if (mysql_error($Query)) { i got like 2000 records in the text file but only 500 get inserted and it stoped with that error. could u help me fix that.Thanks Cheers, 6. ## Re: How to transfer data to mysql from .txt file Originally Posted by tony007 Thank u for u reply. I keep getting the following error : VB Code: Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in \text2mysql.php on line 29 an pointing at this line : if (mysql_error($Query)) {

i got like 2000 records in the text file but only 500 get inserted and it stoped with that error. could u help me fix that.Thanks

Cheers,
That can be removed anyway, its only there for error checking :-)

Try this instead (The variable is not required):

PHP Code:
 <?php    // Presuming you have connected to the DB already...    $Contents = file('yourfile.txt'); foreach($Contents as $Line) {$Query = mysql_query("INSERT INTO tblName (ID, URL) VALUES('', '" . \$Line . "');");      if (!empty(mysql_error())) {        echo mysql_error();      }    }  ?> 
That should work fine presuming each URL is in a file each on its own line.

Cheers,

Ryan Jones

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•