ID: 21198
Updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
-Status: Open
+Status: Feedback
Bug Type: MySQL related
Operating System: Linux
PHP Version: 4.2.3
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2002-12-26 09:45:49] [EMAIL PROTECTED]
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>";
}
?>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=21198&edit=1