mysql load data infile through php script - not working

Andre Ling

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.

Funk Forty Niner

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql LOAD DATA INFILE not working

From Dev

Bash Script for Load Data Infile MySQL

From Dev

LOAD DATA LOCAL INFILE - php 5.5 not working

From Dev

MySQL Load Data Infile

From Dev

MySQL Load Data Infile

From Dev

load data infile syntax in mysql

From Dev

LOAD DATA INFILE MYSQL Error

From Dev

PHP - Import CSV file to mysql database Using LOAD DATA INFILE

From Dev

Loading data into mysql LOAD DATA INFILE difficulty

From Dev

LOAD XML LOCAL INFILE: REPLACE clause working in phpMyAdmin but not from PHP script

From Dev

MySQL LOAD DATA INFILE limits my import?

From Dev

mysql LOAD DATA INFILE NA to NULL transformation

From Dev

mysql LOAD DATA INFILE without FIELDS TERMINATED BY

From Dev

Python/MySQL - LOAD DATA LOCAL INFILE

From Dev

MySQL LOAD DATA LOCAL INFILE to a remote server

From Dev

MySQL/Python LOAD DATA LOCAL INFILE error

From Dev

Equivalent of SET with LOAD DATA INFILE for MySQL 4.1?

From Dev

MySQL LOAD DATA INFILE store line number

From Dev

Python/MySQL - LOAD DATA LOCAL INFILE

From Dev

mysql LOAD DATA INFILE without FIELDS TERMINATED BY

From Dev

mysql LOAD DATA LOCAL INFILE not finding file

From Dev

MySQL LOAD DATA INFILE error 13 on a Pi

From Dev

PHP LOAD DATA INFILE ignore lines

From Dev

MySQL Load Data Infile - Nulls causing error 1265: Data Truncated

From Dev

MySQL 5.6 - load data infile chopping out data (no nulls involved)

From Dev

MySQL LOAD DATA INFILE Data too long for column exception

From Dev

MySQL not working through PHP

From Dev

MySQL LOAD DATA LOCAL INFILE vs. SQL file

From Dev

mysql LOAD DATA LOCAL INFILE with default timestamp column value