Re: php/mySQL search/mysql_num_rows() error



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
.



Relevant Pages

  • Re: Finally which ORM tool?
    ... manipulate the linq query IF you're executing it at that moment. ... simply because the declaration construction was with 'CHOPS'. ... implement IEnumerablebut had an Execute() method which gave back ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: SQL stored procedure executing twice
    ... I wasn't aware that DLookupwould execute the "domain" more than once. ... caused the stored procedure to execute twice. ... Dim stDocName As String ... My pass-thru query properties ...
    (microsoft.public.access.modulesdaovba)
  • Re: Clarification on DBI module
    ... You get the same results for the second execute because the record buffer in the statment handle is not cleaned out because no execute took place. ... The sql query can have bind variables or they may not have. ... Ahhh...imagining that irresistible "new car" smell? ...
    (perl.dbi.users)
  • Re: UPDATE query in Access 2003 raising error
    ... Runtime error 3066 Query must have at least one destination field. ... The SQL works fine if I use it in the QBF Design mode. ... Set qdfTemp = db.CreateQueryDef ... Elsewhere in the code I use the same technique to execute an SQL ...
    (microsoft.public.access.formscoding)
  • Re: Primary Key Violation Error Message
    ... I should have read your query more carefully. ... > Dim ws As DAO.Workspace 'Current workspace (for transaction). ... > 'Step 2: Execute the append. ...
    (microsoft.public.access.formscoding)