What alternatives to mysql_num_rows() can be used in PHP for checking the success of database queries, particularly after an update statement?
When using an update statement in PHP to modify records in a database, the mysql_num_rows() function cannot be used to check the success of the query as it only applies to SELECT statements. Instead, you can use the mysqli_affected_rows() function to determine the number of rows affected by the update query. This function returns the number of rows affected, which can be used to verify the success of the update operation.
// Perform an update query
$update_query = "UPDATE table_name SET column_name = 'new_value' WHERE condition";
if ($mysqli->query($update_query) === TRUE) {
$rows_affected = $mysqli->affected_rows;
if ($rows_affected > 0) {
echo "Update successful. $rows_affected rows affected.";
} else {
echo "No rows were affected by the update.";
}
} else {
echo "Error updating record: " . $mysqli->error;
}
Related Questions
- In PHP development, what steps can be taken to troubleshoot and resolve issues related to duplicate dropdown values based on user language preferences?
- How can POST variables be passed from a selected dataset in PHP?
- What are the limitations of using functions like mime_content_type or finfo_file to determine MIME types in PHP?