Skip to content
PHP Logo

What are some common errors that can lead to a return value of -1 in mysqli_affected_rows in PHP?

One common error that can lead to a return value of -1 in mysqli_affected_rows in PHP is not checking for errors or executing the query properly before calling mysqli_affected_rows. To solve this issue, make sure to check for errors after executing the query and handle them accordingly. Additionally, ensure that the query is executed successfully before calling mysqli_affected_rows.

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

// Check for errors
if(!$result) {
    die("Error: " . mysqli_error($connection));
}

// Check if the query was executed successfully before calling mysqli_affected_rows
if(mysqli_affected_rows($connection) > 0) {
    echo "Update successful";
} else {
    echo "No rows were affected";
}