I am a complete newbie to using MySQL with PHP. (or without for that matter).
I have been trying to load a csv file into a pre-existing table but I keep getting this error:
Access denied for user: 'username@localhost' (Using password: YES)
I have code in the script that verifies that I am connecting to the database and that the file exists. In fact, other scripts with the same username /password work fine. It just seems to be this very important script that wont work. 😕 What am I missing here?
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'username', 'password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully ';
mysql_select_db('username_data') or die('Could not select database');
//define the file and table
$datafile = "/home/sitename/data.csv";
$table = "abc";
//make sure the file exists
if (file_exists($datafile)) {
echo "The file $datafile exists<br />";
} else {
echo "The file $datafile does not exist<br />";
}
// Performing SQL query
$query = "LOAD DATA INFILE '$datafile' REPLACE INTO TABLE $table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n' (Category1, Category2, Category3)";
echo "$query"; //print the query to check what mySQL sees
$result = mysql_query($query) or die('<br />Query failed: ' . mysql_error());
mysql_close($link);
// Free resultset
mysql_free_result($result);
echo 'Task complete ';
// Closing connection
?>