tags:

views:

74

answers:

3

This is the error that I am getting while using mysql_num_rows() function

Error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Here is my source code:

$title = "SELECT * FROM item WHERE item.title LIKE % " .implode("% OR item.title LIKE % ", $data);

$title_result = mysql_query($title, $this->dbh);

echo mysql_num_rows($title_result);

What I am doing is creating a search for a website. Currently I am doing 4 Select statements and then I would like to total everything found in my database from all 4 of those sql statements. If I am thinking correctly the mysql_num_rows() returns the number of rows that was found from a Select statement. So in theory I was thinking that I could add them together to get my total found in a search, am I correct in this thinking if not how could I add 4 different sql select statements together?

Also my test search is in the database.

Thanks in advance for the help.

+1  A: 

Looks like your query failed to execute. You need to check the return value of mysql_query before you try mysql_num_rows.

$title_result = mysql_query($title, $this->dbh);

if($title_result)
{
echo mysql_num_rows($title_result);
}
else
{
die(mysql_error());
}

What actually is happening in your case is mysql_query is returning false, which is not a valid MySQL result resource and when you pass it to mysql_num_rows it returns the following error:

Error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
codaddict
Okay that is apparently my error. So I would assume I am not using the LIKE function correctly. I looked all over google and can't find a good explanation. How does the LIKE work with mysql?
Snader Raider
@Snader: you can see this link for examples: http://www.htmlite.com/mysql011.php
codaddict
A: 

Your MySQL statement did not execute correctly. This is just a hunch, but try again after replacing your $title with the following:

$title = 'SELECT * FROM item WHERE item.title LIKE "%'.implode('%" OR item.title LIKE "%', $data).'%"';

Then later, execute it as follows:

$result = mysql_query ($title, $this->dbh) or die (mysql_error($this->dbh));

Note: this is assuming you trust $data. If it comes from external sources (such as user-specified search), you really ought to sanitise via mysql_real_escape_string or prepared statements

K Prime
The title you suggested works. Thank you. Also another quick question is can you add multiple mysql_num_rows()? My hunch would be yes as it returns an int, but I would like to just confirm my hunch. If My hunch is wrong how could I add multiple Select statements?
Snader Raider
@Snader - Yup, you can add the rows that way. You might be able to do it quicker, though, with a properly crafted SQL statement. Also, if you're interested only in the number of rows, a `COUNT(*)` query would be better.
K Prime
A: 

Thanks a lot that fixed all of my problems. I can't tell you how thankful I am cause I looked at this for about 2 hours trying to figure it out and I was beginning to get very frustrated. :)

Snader Raider