What are the potential reasons for the error message "Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, string given" when using mysqli functions in PHP?
The error message "Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, string given" occurs when the mysqli_affected_rows() function is expecting a mysqli object as its parameter, but instead, a string is being passed to it. This typically happens when the database connection is not properly established before calling the function. To solve this issue, make sure to establish a valid mysqli connection before using the mysqli_affected_rows() function.
// Establish a MySQL database connection
$connection = mysqli_connect("localhost", "username", "password", "database");
// Check if the connection is successful
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Perform database operations
// Close the database connection
mysqli_close($connection);