|
Posted by J.O. Aho on 06/12/06 19:43
Chris wrote:
> I'm working on some search engine code from devpapers.com and keep coming up
> against an error when testing.
>
> This is the error I get:
>
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result
> resource in c:\Inetpub\wwwroot\TMP8j9vfrf3s.php on line 36
> Couldn't execute query
as the result from the mysql_query() can be a resource or FALSE, and in your
case it's a FALSE that is returned and not a resource, which leads
mysql_num_row() to output an error.
> -------------------------------
>
> These are lines 33 through 36:
>
> $query = "SELECT * FROM links WHERE pageURL LIKE \"%$trimm%\" OR
> pageDesc LIKE \"%$trimm%\" OR pageTitle LIKE \"%$trimm%\" ORDER BY pageLink
> DESC" ;
> // Execute the query to get number of rows that contain search
> keywords
> $numresults = mysql_query($query);
> $row_num_links_main = mysql_num_rows($numresults);
>
> -----------------------------------
>
> I'm guessing it's a simple syntax error and I just can't see it after
> looking at it so long, so maybe a different set of eyes will see it.
I suggest you do a small change to your code
//SQL queries uses single quotes to enclose strings
$query = "SELECT * FROM links WHERE pageURL LIKE '%$trimm%' OR
pageDesc LIKE '%$trimm%' OR pageTitle LIKE '%$trimm%' ORDER BY pageLink
DESC" ;
// Execute the query to get number of rows that contain search keywords
$numresults = mysql_query($query);
if($numresults) {
//$numresults is a resource!!!
$row_num_links_main = mysql_num_rows($numresults);
} else {
//$numresults is false
echo "Query: $query<br>\n".mysql_errno().": ".mysql_error()."<br>\n";
exit;
}
This way you see what is wrong with your query or if you have connection
problems. When the page is finished, you may want to store this in a error log
file instead of outputting it to the web page.
//Aho
Navigation:
[Reply to this message]
|