What best practices should be followed when handling database queries in PHP to avoid errors like "Daten wurden geupdatet - betroffen war davon 0 Datensatz"?

When handling database queries in PHP, it is important to properly check for errors and handle them accordingly to avoid issues like "Daten wurden geupdatet - betroffen war davon 0 Datensatz" (Data was updated - 0 records were affected). One common mistake is not checking the return value of the query execution functions to see if any rows were actually affected by the query. To prevent this error message, always check the affected rows after an update query and display a message indicating whether any records were updated.

// Execute the update query
$result = mysqli_query($connection, "UPDATE table SET column = 'new_value' WHERE condition");

// Check if any rows were affected
if(mysqli_affected_rows($connection) > 0) {
    echo "Data was updated successfully.";
} else {
    echo "No records were affected by the update query.";
}