PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { // Create the database if it doesn't exist $temp_pdo = new PDO('mysql:host=' . DB_HOST, DB_USER, DB_PASS, $options); $temp_pdo->exec("CREATE DATABASE IF NOT EXISTS " . DB_NAME); // Now connect to the specific database $pdo = new PDO($dsn, DB_USER, DB_PASS, $options); } catch (PDOException $e) { // In a real application, you would log this error, not display it // For development, it's useful to see the error error_log('Database Connection Error: ' . $e->getMessage()); // Return null or handle the error as appropriate for your application return null; } } return $pdo; } ?>