0

I'm trying to loop over a series of PDO->exec statements and I'm getting weirdness.

If I use a single file of semi-colon seperated valid SQL statements like so:

$sql_file = '/assets/system/default_tables.sql';                    
$sql_queries = file_get_contents($sql_file);
$pdo->exec($sql_queries) or die(print_r($pdo->errorInfo(), true));

my tables get created just hunky-dory fine.

However, if I split-up the large single file of SQL-statements into seperate files and loop over them like so:

$sql_files = glob('/assets/system/*.sql');

foreach($sql_files as $sql_file) {

    $sql_query = file_get_contents($sql_file);
    $pdo->exec($sql_query) or die(print_r($pdo->errorInfo(), true));
}

only the first table is created and I get this pdo error:

Array ( [0] => 00000 [1] => [2] => ) 
  • I have tested the loop, and all the SQL files are read properly.
  • All the seperate SQL files create tables as expected in the single file load code above.

If your wondering why I'm trying to loop over the SQL files... it's mainly because I want to report progress on the creation of each table. I also feel I might have a little more control on possible migration/db update functionality in the future.

If some kind soul can point me in the right direction I'd be very grateful.

0 Answers0