What are common reasons for the error message "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" in PHP code?

The error message "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" typically occurs when the result of a query is not successfully stored in a variable or when there is an issue with the query itself. To solve this issue, ensure that the query is executed correctly and that the result is stored in a variable before using mysql_num_rows() function to count the number of rows returned by the query.

// Make sure to store the result of the query in a variable
$result = mysql_query("SELECT * FROM table_name");

// Check if the query was successful before using mysql_num_rows()
if($result){
    $row_count = mysql_num_rows($result);
    echo "Number of rows: " . $row_count;
} else {
    echo "Error executing query";
}