Error message: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result ...
I have only a little experience with PHP having mostly used straight HTML and CGI/Perl.
I have encountered the following error message when running a query and cannot resolve it, which is a little perplexing!
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in [absolute path to file] on line 120
Line 120 of file=
Code:
$numrows = mysqli_num_rows($result);
The REALLY puzzling thing is that when I run the page and query on my localhost computer (PHP 5.4.7; MySQL 5.5.27) it runs fine and returns a list, just as it should. It's only when the files are transferred to and run on the remote server (PHP 5.2 or 5.4, MySQL 5.0 - 1and1.com) that it fails to run (I have changed all the database accesses and passwords!).
I'm sure it's something simple and stupid, but I'm at a loss! Assistance would be appreciated!!
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM erstallions WHERE $category LIKE '%".$criteria."%'";
$result = mysqli_query($dbcon, $query);
$numrows = mysqli_num_rows($result);
// If the query has results ...
if ($numrows > 0)
{
// ... print out a header
if ($criteria == '')
{
print "<br /><center><font size=\"+2\"><b>Stallion Listing</b></font></center><br>";
}
else
{
print "<center><font size=\"+2\"><b>Information about the stallion '$criteria'<br>(Frozen by <i>Equine-Reproduction.com LLC</i>)</b></font></center><br>";
};
// Fetch each of the query rows
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
// Print one row of results
print "\n<tr>\n\t<td><a href=\"StallionDetails.php?StallionName={$row["StallionName"]}\" target=\"_blank\">{$row["StallionName"]}</a></td>" .
"\n\t<td>{$row["OwnerName"]}</td>" .
"\n\t<td>{$row["StallionBreed"]}</td>" .
"\n\t<td>{$row["StallionRegNo"]}</td>" .
"\n\t<td><center>{$row["DoB"]}</center></td>\n</tr>";
} // end while loop body
// Finish the <table>
print "\n</table>";
} // end if $rowsFound body
// Report how many rows were found
print "<center>{$numrows} record(s) found matching your criteria</center><br>";
You never check to see if mysqli_query() returned boolean FALSE - indicating an error has occurred - and, if so, outputting and/or logging the SQL error message for debugging.
EDIT: Removing the 'or die()' is good - you should never use this construct anyway (it's a pretty poor way of handling errors for both you and your end user). However, you still need to add code that checks what the 'or die()' clause was essentially checking before.
EDIT: Removing the 'or die()' is good - you should never use this construct anyway (it's a pretty poor way of handling errors for both you and your end user). However, you still need to add code that checks what the 'or die()' clause was essentially checking before.
Rather than just calling die(), you might want to configure your code so that you can show an error page. DO NOT show the actual error to the user. Write it to a log file and show the user "an error has been encountered" or something and send a 5XX HTTP code using header (or 404 or something).
BG, perhaps we should put up a sticky about MySQL best practices? A couple of things KEEP coming back:
* DO NOT USE MYSQL_* FUNCTIONS
* CHECK THE RESULTS OF YOUR QUERY
* ESCAPE USER INPUT BEFORE STICKING IT IN A QUERY
Bookmarks