query to upload csv file to table error

Discussion in 'MySQL' started by daiondoroga, Aug 12, 2006.

  1. daiondoroga

    daiondoroga New Member

    Joined:
    Aug 12, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    I am using a webpage that simply runs this query (so client can use it).
    It is supposed to empty the table (the theory being that it resets the auto-incrementing ID to 1 again - which it doesn't!).

    Then it should take the file off the clients desktop, upload it to the server and put it into the table. There's something wrong with the syntax of the second query yet it is EXACTLY the same, apart from the file location, as what DOES work when you use phpmyadmin.

    Tired......look forward to your help. Thanks. Incidentally probably don't need the TRUNCATE query if I use REPLACED in the second one.

    Error given is ....unexpected T_CONSTANT_ENCAPSED_STRING

    {
    // Run query
    mysql_query("TRUNCATE TABLE `books`", $eg_objConn1);
    mysql_query("LOAD DATA LOCAL INFILE 'C:/Documents and Settings/Jon/Desktop/anita.csv' REPLACE INTO TABLE 'books' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\r\n' IGNORE 1 lines", $eg_objConn1);
    }
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Hi,
    You didn't escape the double quotes inside the string, ENCLOSED BY '"'
    Here is the correct query.

    PHP:
    {
    // Run query
    mysql_query("TRUNCATE TABLE `books`"$eg_objConn1);
    mysql_query("LOAD DATA LOCAL INFILE 'C:/Documents and Settings/Jon/Desktop/anita.csv' REPLACE INTO TABLE 'books' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\' LINES TERMINATED BY '\r\n' IGNORE 1 lines"$eg_objConn1);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice