How can the mysql_affected_rows function be used to determine if a database update or insert operation was successful in PHP?
To determine if a database update or insert operation was successful in PHP, you can use the mysql_affected_rows function. This function returns the number of affected rows by the last INSERT, UPDATE, or DELETE query. By checking if the returned value is greater than 0, you can determine if the operation was successful.
// Perform database update or insert operation
$query = "UPDATE table_name SET column_name = 'new_value' WHERE condition";
$result = mysql_query($query);
// Check if the operation was successful
if (mysql_affected_rows() > 0) {
echo "Operation was successful";
} else {
echo "Operation was not successful";
}
Keywords
Related Questions
- What potential pitfalls should be considered when using PHP to interact with database tables and populate form fields dynamically?
- What are the potential security risks of allowing users to upload files to specific folders in PHP?
- What are the best practices for minimizing server load when updating a database frequently with PHP?