How can the use of mysqli functions like mysqli_num_rows() and mysqli_free_result() lead to warnings in PHP scripts?
When using mysqli functions like mysqli_num_rows() and mysqli_free_result(), warnings can occur if the result set is not properly handled. If the result set is not checked before calling mysqli_num_rows(), it can lead to warnings if the result set is empty or if the query fails. Similarly, if mysqli_free_result() is called on a result set that has already been freed or is null, warnings can also be triggered. To avoid these warnings, always check the result set before using mysqli_num_rows() and ensure that mysqli_free_result() is only called on a valid result set.
// Check if the result set is valid before using mysqli_num_rows()
$result = mysqli_query($conn, "SELECT * FROM table");
if($result) {
$num_rows = mysqli_num_rows($result);
// Process the result set
} else {
// Handle query failure
}
// Free the result set only if it is valid
if($result) {
mysqli_free_result($result);
}