Are there specific functions or modules in PHP that are recommended for connecting to different databases?

When connecting to different databases in PHP, it is recommended to use the PDO (PHP Data Objects) extension. PDO provides a consistent interface for accessing different types of databases, such as MySQL, PostgreSQL, SQLite, and more. By using PDO, you can write code that is more portable and secure, as it supports prepared statements to prevent SQL injection attacks.

// Example of connecting to a MySQL database using PDO
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to the database successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}