PDA

Click to See Complete Forum and Search --> : How to transfer data to mysql from .txt file


tony007
May 1st, 2006, 05:42 PM
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

lintz
May 2nd, 2006, 03:03 AM
This should give you an idea....

$fc=file("yourfile.txt");

//loop through file
foreach($fc as $LineInfo) {
//Add your code for inserting a record into your database here
}

tony007
May 2nd, 2006, 10:13 AM
This should give you an idea....

$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 ( ????)

sciguyryan
May 2nd, 2006, 05:56 PM
Thank u for u reply. could u tell me how to refere to each line url in mysql statment? insert into tablename values ( ????)

Something like this (pesuadocode):


<?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

tony007
May 2nd, 2006, 08:01 PM
Something like this (pesuadocode):


<?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 :

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,

sciguyryan
May 3rd, 2006, 03:27 AM
Thank u for u reply. I keep getting the following error :

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
// 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