How does the PHP SAPI (Server Application Programming Interface) affect the functionality of PHP scripts, particularly when handling database connections?

The PHP SAPI affects the functionality of PHP scripts by determining how PHP interacts with the web server. When handling database connections, the PHP SAPI can impact the availability of certain database drivers or modules. To ensure compatibility across different SAPIs, it's important to use a database abstraction layer like PDO or MySQLi for database connections.

// Example of using PDO for database connection
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}