PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); return $pdo; } catch (PDOException $e) { // In a real app, log this error and show a generic message throw new PDOException($e->getMessage(), (int)$e->getCode()); } } function run_migrations() { $pdo = db_connect(); $migrationsDir = __DIR__ . '/migrations'; if (!is_dir($migrationsDir)) { mkdir($migrationsDir, 0775, true); } $migrationFiles = glob($migrationsDir . '/*.sql'); sort($migrationFiles); foreach ($migrationFiles as $file) { try { $sql = file_get_contents($file); if (!empty(trim($sql))) { $pdo->exec($sql); } } catch (PDOException $e) { // Log error, handle failure error_log("Migration failed for file $file: " . $e->getMessage()); } } } // Run migrations automatically on include run_migrations(); // Helper function for easy access function db() { return db_connect(); }