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开发者_JAVA百科 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.
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
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
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. :)
精彩评论