1

I tried so hard to get this command work. Nothing is happening. Here is my issue:

I have a .csv file which I am uploading to my server where I want to run a script to import all of the data into a MySQL table. The csv looks like this:

id;herstellernr;hersteller;firmenname;url
123;ABC;Hersteller1;Firmenname1;http://www.test.com
234;DEF;Hersteller2;Firmenname2;http://www.test2.de
345;GHI;Hersteller3;Firmenname3;http://www.test3.net

... and so on.

After uploading the csv file, I first want to create a new table (temporary) where my data should be imported. Creating the table works without any problems; getting my Data imported from the csv file doesn't.

Here is my script (which comes after the upload script):

$temp = $pdo->prepare("

CREATE TABLE ".$name."
(
   id int(11),
   herstellernr varchar(3),
   hersteller varchar(150),
   firmenname varchar(255),
   url varchar(100),
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

LOAD DATA INFILE '_csv/".$filename."' INTO TABLE ".$name."
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, herstellernr, hersteller, firmenname, url)

");

$temp->execute(array());

CREATE TABLE works but after that there is nothing happening with my LOAD DATA INFILE

Plus: I don't really know if it should be '\n' or '\r\n'. Anyway both cases didn't work.

I tried really hard and I am sure the syntax is just right. But I can't make it. Can you help? Thank you

2
  • 3
    You cannot use multiple queries with PDO. Break each statement into a separate query. You do not need to prepare the query since you're not actually using parameters. A straight query will work fine for each statement. Commented Jul 17, 2018 at 14:25
  • Thank you a lot. I'm not that fit with SQL and PDO. Can you show me a solution with my example?! Would be just GREAT :) Commented Jul 17, 2018 at 14:27

1 Answer 1

2

You cannot use multiple queries with PDO. You'll need to break them up into separate queries. Since you're not using bound parameters, you can skip the prepare, as well:

$pdo->query("CREATE TABLE ".$name."
(
   id int(11),
   herstellernr varchar(3),
   hersteller varchar(150),
   firmenname varchar(255),
   url varchar(100),
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");

$pdo->query("LOAD DATA INFILE '_csv/".$filename."' INTO TABLE ".$name."
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, herstellernr, hersteller, firmenname, url)");
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. This is resulting in Fatal error: Uncaught Error: Call to undefined method PDO::execute() in /www/htdocs/.../:65 Stack trace: #0 {main} thrown in /www/htdocs/.../_update_csv.php on line 65
Which is this line: $pdo->execute("CREATE TABLE ".$name."
Oops, I think that should have been query, not execute. Let me update that.
Now it is creating the table but the data import is still not working :/ 0 rows. Thank you anyway
Another thing to note is that LOAD DATA INFILE needs a full path, and mysql must be able to access that directory. You can use PDO error handling to check for errors after your query to see if it works.
|

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.