Forum Moderators: coopster
$result = mysql_query('LOAD DATA LOCAL INFILE "file.csv" INTO TABLE table1 FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY "\"" IGNORE 1 LINES') ;
note:first line are description fields
When run, it does nothing!
If I issue similar command from shell using mysql client:
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE table1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' IGNORE 1 LINES ;
it loads the file correctly.
I assume it has something to do with use of quotes, but up to now whatever I tried didn't work (from php).
When I did that, the server responded: "Access denied for user: '...@localhost' (Using password: YES)".
All credentials were OK.
I heard that "LOAD DATA INFILE" could be disabled on some servers.
Could that be a problem?
<?
$usr = "db_user";
$pwd = "pass";
$db = "db_db1";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { print "ERROR: " . mysqlerror() . "\n"; }
$select=mysql_select_db($db, $cid);
$delete_old=mysql_query('DELETE FROM table1');
$ld=mysql_query('LOAD DATA INFILE \'file.csv\' INTO TABLE table1 FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' IGNORE 1 LINES') or die(mysql_error());
$cl=mysql_close();
?>
/////////////////////////////////////////////////
The working shell command is almost exactly the same, without mysql part of course, except I added LOCAL (load data local infile...) for using my remote, local PC file.
Right now I setup the cron job for updating db from my local pc, but I am not much happy with that solution.