What are best practices for checking the success of database operations in PHP?

When performing database operations in PHP, it is essential to check the success of these operations to ensure data integrity and proper error handling. One common practice is to use error handling mechanisms provided by PHP database extensions, such as PDO or MySQLi. You can check the return value of database query execution functions to determine if the operation was successful or not.

// Example using PDO
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users');
    $result = $stmt->execute();
    
    if ($result) {
        // Operation was successful
        // Process the retrieved data
    } else {
        // Operation failed
        // Handle the error
    }
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}