|
-
May 1st, 2006, 05:42 PM
#1
Thread Starter
Frenzied Member
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
Last edited by tony007; May 1st, 2006 at 05:46 PM.
-
May 2nd, 2006, 03:03 AM
#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
}
-
May 2nd, 2006, 10:13 AM
#3
Thread Starter
Frenzied Member
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 ( ????)
-
May 2nd, 2006, 05:56 PM
#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 ( ????)
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);
}
}
Cheers,
Ryan Jones
-
May 2nd, 2006, 08:01 PM
#5
Thread Starter
Frenzied Member
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,
-
May 3rd, 2006, 03:27 AM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|