What are best practices for checking if a query returns an error or if mysql_affected_rows is greater than 0 in PHP?
When executing a query in PHP using MySQL, it is important to check if the query returns an error or if mysql_affected_rows is greater than 0 to ensure that the operation was successful. One way to do this is by using conditional statements to check for errors and affected rows after executing the query.
// Execute the query
$result = mysqli_query($connection, $query);
// Check for errors
if (!$result) {
die('Error: ' . mysqli_error($connection));
}
// Check if affected rows is greater than 0
if (mysqli_affected_rows($connection) > 0) {
echo 'Query was successful';
} else {
echo 'No rows were affected';
}