3

I am trying to use the mysql LOAD DATA LOCAL INFILE to get some csv data into my mysql database through a php script using mysqli. This is what my sql string looks like:

LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv' INTO TABLE dashmaker.HHdata FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;

This is what my php script looks like:

$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
       INTO TABLE dashmaker.HHdata
       FIELDS TERMINATED BY ','
       OPTIONALLY ENCLOSED BY '\"' 
       LINES TERMINATED BY '\n' 
       IGNORE 1 LINES;";

$con=mysqli_connect("localhost","[user]","[password]","[database]");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};

$result = mysqli_query($sql, $con);

if (mysql_affected_rows() == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysql_error(); 
};

echo $message;

mysqli_close($con);

I found that I needed to set the mysql my.cnf to include local-infile under [mysql] and [mysqld] - so I have done that.

When I run the sql query through the shell it works. When I try to do it through the php script the error message ($message) I now get says:

The user update failed: Access denied for user ''@'localhost' (using password: NO) 

One weird thing is that it doesn't show any user name before the @'localhost'. Can't understand why. Besides this, I use the same connection setting to run regular SELECT queries from the database using php scripts. The user also has FILE privileges.

I have searched extensively but haven't found anything that can explain what's going on. Any advice would be much appreciated.

4
  • 1
    You're mixing MySQL APIs with mysql_ and mysqli_ functions. They do not mix; use mysqli_ exclusively. Plus, you're not using brackets in [user] etc, are you? This isn't MSSQL, it's MySQL, remove them. Commented Oct 8, 2014 at 6:00
  • 1
    Plus, in mysqli_, DB connection comes first, invert these $result = mysqli_query($sql, $con); to read as $result = mysqli_query($con, $sql); Commented Oct 8, 2014 at 6:03
  • 1
    Thank you! Incredibly quick response! It is working. I think the error message was also throwing me off - mysql_affected_rows() is now mysql_affected_rows($con)... and I wasn't using square brackets for user/password - just meant to indicate these were placeholders. Thanks again! Commented Oct 8, 2014 at 6:17
  • You're welcome Andre. I posted an answer for you below. Commented Oct 8, 2014 at 6:18

1 Answer 1

8

You're mixing MySQL APIs with mysql_ and mysqli_ functions in a few instances.

  • mysql_affected_rows()
  • mysql_error()

They do not mix together; use mysqli_ exclusively.

Plus, you're not using brackets in [user] etc, are you? That is MSSQL syntax, remove them.

Plus, in mysqli_, DB connection comes first, invert these $result = mysqli_query($sql, $con); to read as $result = mysqli_query($con, $sql);

$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
       INTO TABLE dashmaker.HHdata
       FIELDS TERMINATED BY ','
       OPTIONALLY ENCLOSED BY '\"' 
       LINES TERMINATED BY '\n' 
       IGNORE 1 LINES;";

$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};

$result = mysqli_query($con, $sql);

if (mysqli_affected_rows($con) == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};

echo $message;
mysqli_close($con);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.