ID:               21198
 Updated by:       [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
 Status:           Bogus
 Bug Type:         MySQL related
 Operating System: Linux
 PHP Version:      4.2.3
 New Comment:

Please ignore everything I said and go here:

http://www.php.net/support

This is not a bug in PHP.


Previous Comments:
------------------------------------------------------------------------

[2002-12-26 12:07:08] [EMAIL PROTECTED]

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 11:49:50] [EMAIL PROTECTED]

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 10:55:15] [EMAIL PROTECTED]

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 10:39:28] [EMAIL PROTECTED]

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 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

Reply via email to