How can the error message "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" be resolved in PHP 5 when querying a database?
The error message "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" occurs when the query doesn't return a valid result set. To resolve this issue, you need to check if the query was successful before calling mysql_num_rows() function. This can be done by checking the return value of mysql_query() function to ensure it's a valid result resource.
// Perform the query
$result = mysql_query($query);
// Check if the query was successful
if($result){
// Use mysql_num_rows() function to get the number of rows
$num_rows = mysql_num_rows($result);
echo "Number of rows: $num_rows";
} else {
echo "Error executing query: " . mysql_error();
}
Keywords
Related Questions
- In what scenarios would it be recommended to use JavaScript to send POST data instead of traditional form submission in PHP?
- What are the best practices for handling user input in PHP to prevent XSS vulnerabilities?
- What are the best practices for handling errors in PHP scripts to ensure the execution continues smoothly?