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.

Krish R
  • 22,583
  • 7
  • 50
  • 59

1 Answers1

2
$dirf    = 'mysqls';
$dir = scandir($dirf);
foreach($dir as $file) {
   if(($file!='..') && ($file!='.')) {
       $sql = file_get_contents($file);
       $qr = $dbh->exec($sql);
   }
}
Bhadra
  • 2,121
  • 1
  • 13
  • 19
  • How would I be able to show an success / error message? – user3001214 Nov 18 '13 at 07:04
  • success message for db query?? – Bhadra Nov 18 '13 at 07:05
  • success/error depends on the db query only.so you can echo the success/error of db query in the if loop – Bhadra Nov 18 '13 at 07:08
  • 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. – user3001214 Nov 18 '13 at 07:22
  • If it helps, in my .sql files I have similar codes of "INSERT INTO `test` (`id`, `title`) VALUES (1, 'hello');" – user3001214 Nov 18 '13 at 07:25
  • i think in .sql files there are so many queries.I think php needs some other func , other than ->exec to run these.once check how to run multiple queries.check This http://stackoverflow.com/questions/6346674/pdo-support-for-multiple-queries-pdo-mysql-pdo-mysqlnd – Bhadra Nov 18 '13 at 07:30
  • even if I use only 1 .sql it still does not work. – user3001214 Nov 18 '13 at 08:08