1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP CSV Upload Error

Discussion in 'PHP' started by kasun0777, Jun 7, 2011.

  1. #1
    i want to upload my csv file to mysql db.

    $query = "LOAD DATA LOCAL INFILE '1.csv' INTO TABLE test FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (uid, name, tel)";
    $result = mysql_query($query); 
    Code (markup):
    1.csv saved in this coding file csv.

    This is csv data
    
    22,Kasun Chamara,89975954
    55,Dsukfefk,5945945665
    42,vewgfr,45644562162
    Code (markup):
    but not upload to db. help me
     
    kasun0777, Jun 7, 2011 IP
  2. kasun0777

    kasun0777 Well-Known Member

    Messages:
    355
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #2
    im using windows 7 and xampp
     
    kasun0777, Jun 7, 2011 IP
  3. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try it from phpmyadmin and see the error mesage
     
    jazzcho, Jun 7, 2011 IP
  4. kasun0777

    kasun0777 Well-Known Member

    Messages:
    355
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #4
    i resolved that pro
    $query = "load data local infile 'c:/1.csv' into table test fields terminated by ',' lines terminated by '\n' (uid, name, tel)";
    $result = mysql_query($query);

    but. if i send query again, get duplicate csv datas.
    do u knw how to update database with over righting.
     
    kasun0777, Jun 7, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    just empty the table and then reinsert it
     
    srisen2, Jun 7, 2011 IP
  6. kasun0777

    kasun0777 Well-Known Member

    Messages:
    355
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #6
    i thought that. and this is my code

    <?php
    $sql = "SELECT * FROM test";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) > 0){
    mysql_query("DELETE FROM test");
    mysql_query("ALTER TABLE test AUTO_INCREMENT = 1");
    $query = "load data local infile '/1.csv' into table test fields terminated by ',' lines terminated by '\n'(uid, name, tel)";
    mysql_query($query);
    echo "Update Data Success";
    }
    else{
    mysql_query("ALTER TABLE test AUTO_INCREMENT = 1");
    $query = "load data local infile '/1.csv' into table test fields terminated by ',' lines terminated by '\n'(uid, name, tel)";
    mysql_query($query);
    echo "Added New Data Successful";
    }
    ?>

    Thank You
     
    kasun0777, Jun 7, 2011 IP
  7. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Instead of using 'load data' sql query, you can use a custom CSV parser. This will allow you to easily strip duplicates and do other kind of validations. Example is as follows:
    
    $str = file_get_contents("1.csv");
    $lines = explode("\n",$str);
    for($i=0;$i<count($lines);$i++)
    {
      list($uid,$name,$tel) = explode("," , $lines);
      $data = mysql_query("SELECT * FROM users WHERE uid='".$uid."' LIMIT 1");
      if(mysql_num_rows($data)>0)
      {
         $query = "UPDATE users SET name='".$name."', tel='".$tel."' WHERE uid='".$uid."' LIMIT 1";
      }
      else
      {
         $query = "INSERT INTO users (uid, name, tel) VALUES ('".$uid."', '".$name."', '".$tel."')";
      }
      mysql_query($query);
    }
    
    PHP:
    Although, I do admit that this piece of code is much more performance costly. It'll take much more time than "load data" query.
     
    techbongo, Jun 9, 2011 IP