What best practices should be followed to ensure that a PHP script runs smoothly on multiple systems when interacting with databases?

To ensure a PHP script runs smoothly on multiple systems when interacting with databases, it's important to use parameterized queries to prevent SQL injection attacks, handle errors gracefully, and ensure compatibility with different database systems by using PDO or mysqli for database connections.

// Example code snippet using PDO for database connection and parameterized queries
try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();
    
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Process results
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}