Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow 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.

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

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

    Quote 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. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

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

    Quote 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
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

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

    Quote 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:
    1. 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. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

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

    Quote Originally Posted by tony007
    Thank u for u reply. I keep getting the following error :

    VB Code:
    1. 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
    My Blog.

    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
  •  



Click Here to Expand Forum to Full Width