Closed Thread
Results 1 to 5 of 5

Thread: How to Add Multiple Lines of .csv File Into Database

  1. #1
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    How to Add Multiple Lines of .csv File Into Database

    Hey guys,

    Once again I have encountered a slight problem I cannot seem to overcome - Id be really grateful if you could help me out (again!, this forum is great - I promise Ill contribute when I get enough knowledge).

    Basically, I have a csv file - here's an example of the data in it:

    Code:
    id,title,category,sub,description,tag,price,imgs,imgl,link,source,sourceimg,status
    9,Regulated Linear Single Output DC Power supply 5v 3Amp,"Cables, Parts & Power Supplies",43-Power Supplies,"Linear Power Supply designed to meet EN60950. Equivalent to EMS Ref D500. Input 100-240Vac so as compatible with power supplies thoughout the world. Remote sense facility to compensate for voltage drop across load leads. Output connections via fast on terminals with 0.25inch and 0.11inch connectors. Adjustable output voltage with low ripple. Over voltage protection on 5v output. Cover kit available, please ask if required.",R3 Technology,49.7,,http://images.productserve.com/preview/239/571256.jpg,http://www.awin1.com/pclick.php?p=571256&a=72979&m=239,Budget Batteries,,1
    10,Regulated Linear Single Output DC Power supply 12-15v 6.8A,"Cables, Parts & Power Supplies",43-Power Supplies,"Linear Power Supply designed to meet EN60950. EMS Ref D505 Input 100-240Vac so as compatible with power supplies thoughout the world. Remote sense facility to compensate for voltage drop across load leads. Output connections via fast on terminals with 0.25inch and 0.11inch connectors. Adjustable output voltage with low ripple. Cover kit available, please ask if required.",R3 Technology,115.82,,http://images.productserve.com/preview/239/571257.jpg,http://www.awin1.com/pclick.php?p=571257&a=72979&m=239,Budget Batteries,,1
    11,Regulated Linear Single Output DC Power supply 24-28v 1.2A,"Cables, Parts & Power Supplies",43-Power Supplies,"Linear Power Supply designed to meet EN60950. EMS Ref D506 Input 100-240Vac so as compatible with power supplies thoughout the world. Remote sense facility to compensate for voltage drop across load leads. Output connections via fast on terminals with 0.25inch and 0.11inch connectors. Adjustable output voltage with low ripple. Cover kit available, please ask if required.",R3 Technologies,47.15,,http://images.productserve.com/preview/239/571258.jpg,http://www.awin1.com/pclick.php?p=571258&a=72979&m=239,Budget Batteries,,1
    I renamed it from the .csv to .txt and uploaded it to my server

    Anyway, I have this code to try and add it into my database:

    Code:
    	$query = "LOAD DATA LOCAL INFILE 'the_feed.txt' INTO TABLE tbprofo FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
    	$result1 = mysql_query($query, $profoconnection) or die(mysql_error());
    The only problem is that it only adds one line of my .txt file - the first line:

    Code:
    id,title,category,sub,description,tag,price,imgs,imgl,link,source,sourceimg,status
    The irony is that this first line is the heading, and not actually any proper data.

    So my question - does anyone know how to edit my code so that it adds all the data in my csv file?

    Thanks guys!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: How to Add Multiple Lines of .csv File Into Database

    Id make your own script to do it. Open it, break it by line then do something like:

    Code:
    <?php
    $file 
    explode("\n"file_get_contents('file.txt'));
    $headers array_shift($file); //headers
    foreach($file as $line) {
    //query
        
    $query mysql_query("INSERT INTO example ($headers) VALUES ($line)");
    }
    ?>
    Obviously wont work out of the box but I am sure you get a good idea for it.

  4. #3
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Re: How to Add Multiple Lines of .csv File Into Database

    thank you blainesch

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: How to Add Multiple Lines of .csv File Into Database

    Your welcome, let us know if anything does not work correctly.

    +Rep appreciated.

  6. #5
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Re: How to Add Multiple Lines of .csv File Into Database

    In an effort to attempt to help others in my situation, here is the code in order to import csv data into mysql database:

    <?php

    $datanames = file_get_contents("datanames.csv");
    $txt = preg_replace('/\r\n|\r/', "\n", $datanames);
    $file = explode("\n", $txt);

    $numlines = count($file);

    for($i=0; $i<$numlines; $i++)
    {
    echo "Line ".$i.": ".$file[$i]."<br />";
    }



    for($l=0; $l<$numlines; $l++)
    {
    $data = explode(",", $file[$l]);
    $numcells = count($data);

    //set all the data for $madata
    for($i=0; $i<$numcells; $i++)
    {
    $madata[$l][$i] = $data[$i];
    echo $madata[$l][$i]."<br />";
    }
    }
    //now add the data in $madata into the db
    echo "now add the data in $madata into the db<br />";
    for($x=0; $x<(count($file)); $x++)
    {
    for($y=0; $y<count($data); $y++)
    {
    $query = "INSERT INTO sp_table (name, telephone, birthday) VALUES ('".$madata[$x][$y]."', '".$madata[$x][($y+1)]."', '".$madata[$x][($y+2)]."')";
    echo $query."<br />";
    if(!mysql_query($query, $scrappad_connection))
    {
    die('error: '.mysql_error());
    }
    break;
    }
    }

    ?>
    this code works perfectly for me, hopefully you can make use of it to

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to erase parts of lines of an entire file?
    By onething in forum General Programming
    Replies: 3
    Last Post: 03-21-2011, 03:43 PM
  2. adding multiple images into a MySQL database with PHP
    By whitestar in forum PHP Development
    Replies: 17
    Last Post: 03-09-2011, 05:41 PM
  3. Problem reading separate lines from a file.
    By TheUmer in forum C and C++
    Replies: 7
    Last Post: 03-27-2010, 07:47 AM
  4. C-reading multiple lines
    By geeko in forum C and C++
    Replies: 1
    Last Post: 10-19-2008, 12:23 PM
  5. Removing lines from a file
    By NeedHelp in forum C and C++
    Replies: 1
    Last Post: 11-01-2006, 10:10 AM

Tags for this Thread

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