2

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.

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

2 Comments

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

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.