0

I'm trying to import a csv file to my mysql database, but I can't get it to work. I looked up a lot of solutions on this website, but none of them seems to solve it.

I'm getting the error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LOAD DATA INFILE 'name.csv' INTO TABLE test FIELDS TERMINATED BY ',' ' at line 2.

This is my name.csv file and my php code:

productId,ean,title
10060,8720094110226,KATRIZ Epson 
10059,8720082041877,Lexmark 14L0198

$link = mysqli_connect("localhost", "username", "password", "");

$query = <<<eof
USE `db_name`;
LOAD DATA INFILE 'name.csv'
 INTO TABLE `tablename`
 FIELDS TERMINATED BY ','
 LINES TERMINATED BY '\n'
(productId,ean,title)
eof;

if ($result = mysqli_query($link, $query)) {
  echo "successful";
} else {
  printf("Errormessage: %s\n", mysqli_error($link));
};

I expect SQL to upload the name.csv file into my table tablename, but it's not working. My connection to the database gives no errors.

Jerooney
  • 19
  • 2
  • 1
    You need a semi-colon after `USE db_name` when joining commands like that. – aynber Apr 17 '19 at 15:02
  • Added the semi-colon, but still the same error. – Jerooney Apr 17 '19 at 15:06
  • Unless you're using mysqli_multi_query, it will have issues using 2 commands. Instead, use [mysqli_select_db](https://www.php.net/manual/en/mysqli.select-db.php) after your connect, or pass the db_name into the [mysqli_connect](https://www.php.net/manual/en/mysqli.construct.php) command. – aynber Apr 17 '19 at 15:17
  • I tried that, but that makes me run into the error: Access denied for user 'xlfnxgbs_prod'@'localhost' (using password: YES) Also, when I run this query in phpmyadmin, without the use-line, it gives me the same SQL syntax error. – Jerooney Apr 17 '19 at 15:26
  • Possible duplicate of [MYSQL import data from csv using LOAD DATA INFILE](https://stackoverflow.com/questions/14127529/mysql-import-data-from-csv-using-load-data-infile) – Rahul Singh Apr 17 '19 at 16:07
  • Tried that, didn't work for me. – Jerooney Apr 17 '19 at 17:54

0 Answers0