2

I am trying to create a script where I can import .sql files from multiple directories into my database.

I am able to import one SQL by this code:

<?php
$sql = file_get_contents('mysqls/hey.sql');
$qr = $dbh->exec($sql);
?> 

But when I move on to make it read more then one .sql file it dosnt work:

<?php
$sql = file_get_contents('mysqls/*.sql');
$qr = $dbh->exec($sql);
?> 

I would also like for it to go into multiple folders that may be created in the future [folders would be in the same directory as the code].

My database connection has already been made in a PDO config connection.

1
  • you need to traverse through each file Commented Nov 18, 2013 at 6:50

1 Answer 1

2
$dirf    = 'mysqls';
$dir = scandir($dirf);
foreach($dir as $file) {
   if(($file!='..') && ($file!='.')) {
       $sql = file_get_contents($file);
       $qr = $dbh->exec($sql);
   }
}
Sign up to request clarification or add additional context in comments.

7 Comments

How would I be able to show an success / error message?
success message for db query??
success/error depends on the db query only.so you can echo the success/error of db query in the if loop
Dont worry, relised how to do then removed as I no longer need the messages. Anyways the code you provided is not working correctly. Its reading the files, but for some reason its not running it through the database.
If it helps, in my .sql files I have similar codes of "INSERT INTO test (id, title) VALUES (1, 'hello');"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.