|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-12-26 09:45 UTC] chris1075 at netzero dot com
I have a very simple PHP script, that searches a small mysql database for a result. And the result that I keep receiving pertaining to the mysql_num_rows() function is listed below.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /bookorama/results.php on line 29
Number of books found:
Here is some of the PHP script.
<?
trim($searchterm);
if(!$searchtype || !$searchterm) {
echo "You have not entered search details. Please go back and try again.";
exit;
}
$searchtype = addslashes($searchtype);
$searchtype = addslashes($searchterm);
@ $db = mysql_connect("localhost", "headdive_jazz", "jazz");
if(!$db) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("books");
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_result = mysql_num_rows($result);
echo "<p>Number of books found: ".$num_results."</p>";
for($i=0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
echo "<p><strong>".($i + 1).". Title: ";
echo htmlspecialchars(stripslashes($row["title"]));
echo "</strong><br>Author: ";
echo htmlspecialchars(stripslashes($row["author"]));
echo "<br>ISBN: ";
echo htmlspecialchars(stripslashes($row["isbn"]));
echo "<br>Price: ";
echo htmlspecialchars(stripslashes($row["price"]));
echo "</p>";
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 12:00:01 2025 UTC |
Rewrite this: $result = mysql_query($query); Like this: if (!$result = mysql_query($query)) { print "Could not run query ($query) : " . mysql_error(); exit; } If that doesn't output something, try and rewrite your query like so: $query = "SELECT count(*) FROM books WHERE $searchtype LIKE '%$searchterm%'"; And get the count by: if ($result = mysql_query($query)) { print mysql_result($result,0); exit; } Anyway this looks like a support question but just in case try the above. I'm guessing the query is invalid, like, $searchtype is not defined correctly or something. In which case you'd be giving mysql_num_rows() a invalid mysql result resource because mysql_query() retured false on the bogus query. And BTW, you want && not ||. Error handling is your friend. Like for example, make sure $searchtype is a column you want to use. Anyway, when in doubt, print stuff.This is what my code looks like now: <? trim($searchterm); if(!$searchtype || !$searchterm) { echo "You have not entered search details. Please go back and try again."; exit; } $searchtype = addslashes($searchtype); $searchterm = addslashes($searchterm); @ $db = mysql_connect("localhost", "headdive_jazz", "jazz"); if(!$db) { echo "Error: Could not connect to database. Please try again later."; exit; } mysql_select_db("books"); $query = "SELECT count(*) FROM books WHERE $searchtype LIKE '%$searchterm%'"; if(!$result = mysql_query($query)) { print mysql_result($result, 0); exit; } $num_result = mysql_num_rows($result); echo "<p>Number of books found: ".$num_results."</p>"; for($i=0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo "<p><strong>".($i + 1).". Title: "; echo htmlspecialchars(stripslashes($row["title"])); echo "</strong><br>Author: "; echo htmlspecialchars(stripslashes($row["author"])); echo "<br>ISBN: "; echo htmlspecialchars(stripslashes($row["isbn"])); echo "<br>Price: "; echo htmlspecialchars(stripslashes($row["price"])); echo "</p>"; } ?> and this is the response I get: Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /bookorama/results.php on line 29