Closed Thread
Results 1 to 9 of 9

Thread: [php] Importing a file?

  1. #1
    b& Zombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond repute
    Join Date
    Mar 2007
    Location
    Florida
    Posts
    1,985

    [php] Importing a file?

    I have a text file containing 8000 ID's and names I need to import to sql and I really don't wanna do it by hand... I first thought of doing the ID's first then the name later which would work except I don't know myself how to read a file and insert each line into the database and can't find anything online...

    [Actually thinking of it, I could just create the ID's to the database with a while() but the names are still an issue...]

    Can anyone assist in pulling a text file w/ multiple lines into a database?

  2. #2
    Junior Member jaimunki is infamous around these parts
    Join Date
    Mar 2006
    Posts
    22
    google: "php parse text file to mysql" or even "php parse text file to mysql line by line"

    else, your going to need to dump a few lines of the file (change sensitive info, but leave structure in place) in here so we can see the exact format and any delimiters etc.

  3. #3
    b& Zombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond repute
    Join Date
    Mar 2007
    Location
    Florida
    Posts
    1,985
    ID LIST: http://pastebin.com/LRs0S0xC
    [Imagine the spaces being | and no NPC ID: at the top]

    Code attempt 2 [2 Attempts, similar code, both failed]
    Code:
    <?php
    include "config.php";
    ERROR_REPORTING(E_ALL);
    $data = fopen("c:/xampp/htdocs/rs/site/npcid.txt", 'r');
    
    $information =  fread($data, filesize("c:/xampp/htdocs/rs/site/npcid.txt"));
    $array = explode(" \r\n ", $information);
    print_r($array);
    $count = count($array);
    echo $count;
    for($i=0; $i==$count; $i++) {
    echo $i;
    	$data = explode('|', $array[$i]);
        mysql_query("INSERT INTO `site_npcids` ( id, npc) VALUES ('{$data[0]}', '{$data[1]}')") 
    
    or die('failed somewhere'); 
    }
    ?>
    Attempt 1
    Code:
    <?php
    mysql_pconnect("localhost","root","------");
    mysql_select_db("-------");
    ERROR_REPORTING(E_ALL);
    $data = fopen("c:/xampp/htdocs/rs/site/npcid.txt", 'r');
    
    $information =  fread($data, filesize("c:/xampp/htdocs/rs/site/npcid.txt"));
    $closeit = fclose($data);
    //list($id, $npc) = print_r(explode('|', $information, 2));
    print_r(explode('|', $information, 2));
    echo "ID: $array[0]<br />"; // foo
    echo "NPC: $array[1]<br />"; // *
    //$count = round(count($array) / 2);
    //echo $count;
    //for($i=0; $i==$count; $i=$i+2) {  
    //     mysql_query("INSERT INTO `site_npcids` ( id, npc) VALUES ('{$array[$i]}', '{$array[$i //+ //1]}')") or die('failed somewhere');  
    //}
    
    ?>
    Exert of the page result
    Array ( [0] => 0|Hans 1|Man 2|Man 3|Man 4|Woman 5|Woman 6|Woman 7|Farmer 8|Thief 9|Guard 10|Schoolgirl 11|Tramp 12|Barbarian 13|Wizard 14|Druid 15|Warrior woman 16|Man 17|Schoolgirl 18|Al-Kharid warrior 19|White Knight 20|Paladin 21|Hero 22|Forester 23|Knight of Ardougne 24|Man 25|Woman 26|Knight of Ardougne 27
    Nothing posted to the database, nor formatted correctly since the npcid.txt is formatted ID|NAME

    Any assistance?
    Last edited by Zombie; May 15th, 2011 at 12:14.

  4. #4
    Member sunnyverma1984 is an unknown quantity at this point
    Join Date
    Apr 2007
    Location
    India
    Posts
    79
    you can do this via phpmyadmin using csv import
    worth review website worth
    toolspot collection of free webmasters tools

  5. #5
    It gets better. Dynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant futureDynash has a brilliant future
    Join Date
    Jan 2008
    Location
    United Kingdom
    Posts
    4,534
    In your attempt one you have your db inserts commented out - but you have your prints uncommented. That's why you see Array ( [0] => 0|Hans 1|Man 2|Man 3|Man 4 etc, and nothing is imported.
    JavaKrypt

    "If you think education is expensive. You should see the cost of ignorance."

  6. #6
    b& Zombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond reputeZombie has a reputation beyond repute
    Join Date
    Mar 2007
    Location
    Florida
    Posts
    1,985
    Well at first the database wasn't commented out at first, and at first it didn't work either. It was just commented out cause I was just trying to figure out how to get it to format correctly anyway, I had my friend helping me through some of it and neither of us were able to get it to work...

  7. #7
    Jay Street iBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond repute iBrightDev's Avatar
    Join Date
    Oct 2005
    Location
    Not sure, need a GPS.
    Posts
    7,138

    hope this helps

    PHP Code:
    $handle $fopen("file.txt""r");
    $conn mysql_connect("localhost","user","pass");
    mysql_select_db("dbname",$conn);

    while (!
    feof($handle)) { //this will loop till end of file

    $buffer fgets($handle4096);
    list(
    $a,$b)=explode("|",$buffer);

    echo 
    "{$a} - {$b}<br/>";

    $sql "INSERT INTO table (id,other_column_name) VALUES('".$a."','".$b."')";
    mysql_query($sql,$conn) or die(mysql_error());


    that should work for what you need, but, without seeing all your stuff, im just guessing.

    *edit
    sorry, typo in query. fixed now
    Last edited by iBrightDev; May 17th, 2011 at 10:28.
    Full-service digital agency based in Scottsdale, Arizona - iBright Development

  8. #8
    Junior Member Tanika has a little shameless behaviour in the past
    Join Date
    May 2011
    Posts
    18

    Exclamation

    Great Stuff Friend. I too have the same query in my project where i need import all the student register number more than 1000. Instead of importing, you have any idea of generating the ids to student from particular number to another number. For example, consider 232212 - 233212.
    So, i will enter the start and ending of register number only but it will automatically generate and assign from the top of the table till the end for all students.

  9. #9
    Jay Street iBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond reputeiBrightDev has a reputation beyond repute iBrightDev's Avatar
    Join Date
    Oct 2005
    Location
    Not sure, need a GPS.
    Posts
    7,138
    if you need to know where the last student number is, are you having it as an auto increment in the DB? if not, so you know what number you want to start with? either way, you will need to set what the first one will be, weather auto or hardcoded, then do a for loop that will add +1 to the count. you will also have to count how many things need to be inserted and tell the for loop to break at that number to return a successful message.
    Full-service digital agency based in Scottsdale, Arizona - iBright Development

Closed Thread

Similar Threads

  1. Replies: 1
    Last Post: July 16th, 2007, 06:52
  2. Webhosting and importing needed
    By ndcelario in forum Paid hosting requests
    Replies: 10
    Last Post: April 4th, 2006, 13:28
  3. Importing SQL in MySQL database
    By Force in forum Programming Help
    Replies: 3
    Last Post: May 14th, 2002, 19:45
  4. importing mass favs to mozila?
    By is0lized in forum General Discussions
    Replies: 8
    Last Post: March 21st, 2002, 15:09

Bookmarks

Posting Permissions

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