php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21198 mysql_num_rows() not valid
Submitted: 2002-12-26 09:45 UTC Modified: 2002-12-26 12:14 UTC
From: chris1075 at netzero dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 4.2.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chris1075 at netzero dot com
New email:
PHP Version: OS:

 

 [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>";
	}
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-26 10:39 UTC] philip@php.net
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.
 [2002-12-26 10:55 UTC] chris1075 at netzero dot com
Okay, I did the first rewrite and I got the below output.
 Could not run query (select * from books where Java 2 like '%Java 2%') : You have an error in your SQL syntax near '2 like '%Java 2%'' at line 1

Then I tried the second rewrite and I got the one below.
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /bookorama/results.php on line 32
 [2002-12-26 11:49 UTC] philip@php.net
You can't have spaces in column names, your query is bogus.  And btw I just noticed your typo:

$searchtype = addslashes($searchtype);
$searchtype = addslashes($searchterm);  // notice the problem?

Anyway, this is bogus.
 [2002-12-26 12:07 UTC] chris1075 at netzero dot com
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
 [2002-12-26 12:14 UTC] philip@php.net
Please ignore everything I said and go here:

http://www.php.net/support

This is not a bug in PHP.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 00:01:36 2025 UTC