Can someone please help? I have been working on this for a couple of days now and still cannot seem to get a php script which imports a .csv file into an existing table.

The file imports just fine when i do it manually in phpmyadmin. I just dont know why writing the syntax into the php file makes it not work.

This is how each line in the .csv file is formatted (5 fields):
"Donald Jones","22 Bombay Ave,Springs,FL,32222","Apples",0.0,0.0

Here is what I put to get it to work manually in phpmyadmin:

Here is what my php has which doesnt enter the info into the table:

<?php

$conn = mysql_connect('xxxx','xxxx','xxxx');

mysql_select_db('ryanwoo_map');

mysql_query("TRUNCATE TABLE ryanwoo_map") or die(mysql_error());

mysql_query("LOAD DATA INFILE 'fruitlist.csv'
INTO TABLE ryanwoo_map(
name,
address,
fruit,
lat,
lng)
LINES TERMINATED BY '\r\n'
FIELDS TERMINATED BY ','
FIELDS ESCAPED BY '\'
ENCLOSED BY '\"'");


?>

    how about checking for errors with mysql_error();

      dagon;10969648 wrote:

      how about checking for errors with mysql_error();

      Here is the error given:

      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LINES TERMINATED BY ' ' FIELDS TERMINATED BY ',' FIELDS ESCAPED BY '\' FIELDS E' at line 8

        try

        mysql_query('LOAD DATA INFILE "fruitlist.csv"
        INTO TABLE ryanwoo_map(
        name,
        address,
        fruit,
        lat,
        lng)
        LINES TERMINATED BY "\r\n"
        FIELDS TERMINATED BY ","
        FIELDS ESCAPED BY "\"
        ENCLOSED BY "'" ');

          You may need to escape the back-slashes in cases where you want them to appear in the query itself, e.g. "\r\n".

          PS: Or see if inverting the quote types as Dagon suggests takes care of it.

            I have tried everything. This is my first time writing a mysql query inside of php. I have read the manual many times but cannot seem to get it to work and/or understand it.

              Write a Reply...