What are the best practices for handling database operations in PHP applications, especially when it involves altering table structures?

When handling database operations in PHP applications, especially when altering table structures, it is important to use proper error handling techniques to ensure the changes are executed successfully. One best practice is to wrap the alter table query in a try-catch block to catch any potential errors that may occur during the operation.

try {
    $conn = new PDO("mysql:host=localhost;dbname=myDB", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "ALTER TABLE myTable ADD COLUMN newColumn VARCHAR(255)";
    $conn->exec($sql);

    echo "Table altered successfully";
} catch(PDOException $e) {
    echo "Error altering table: " . $e->getMessage();
}