Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Page 1

Status
Not open for further replies.

manicleek

Technical User
Joined
Jun 16, 2004
Messages
143
Location
GB
I am trying to build a search page on my site.

On the search page you can search by town and/or
marina name.

On the results page I have the following code:

Code:
$townsearch = $_POST['townsearch'];
$marinanames = $_POST['marinanamesearch'];

//Details SQL Query
$detailsquery = "SELECT * FROM marinas WHERE country = 'UK' AND town LIKE ".$townsearch." AND marinaname LIKE '%".$marinanamesearch."%' ORDER BY listingtype ASC, marinaname ASC";
$detailsresult = mysql_query($detailsquery, $connection);
$detailsnumber = mysql_num_rows($detailsresult);

this brings up the error:


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in results.php on line 15


when I do an error check it tells me


Unknown column 'Fleetwood' in 'where clause'


Fleetwood being the town I tried to search by
 
there is a problem with ur query, try this:
Code:
$detailsquery = "SELECT * FROM marinas WHERE country = 'UK' AND town LIKE ".$townsearch." AND marinaname LIKE '%".$marinanamesearch."%' ORDER BY listingtype ASC, marinaname ASC";

echo $detailsquery;

execute the sql that is outputted in ur database, this is not a php error....

Known is handfull, Unknown is worldfull
 
Start adding error checking to your mysql commands:
Code:
$detailsresult = mysql_query($detailsquery, $connection)[COLOR=red] OR die("Query failed: ".mysql_error())[/color];
 
I have error checking, I posted the errors that it came up with
 
You are relying on the settings of the PHP installation. Move your code somwhere else and the errors messages might disappear. Explicit error checking is a better idea than relying on various settings in PHP.ini
If you build an application of high quality you'd also want to make it fail gracefully rather than displaying error messages to the user.
 
I agree with vbkris,
the best way to do error checking for these problems is to simply echo your query. However I think I may have spotted the mistake:

Code:
$detailsquery = "SELECT * FROM marinas WHERE country = 'UK' AND town LIKE ".$townsearch." AND marinaname LIKE '%".$marinanamesearch."%' ORDER BY listingtype ASC, marinaname ASC";

may work if you also add ' ' to $townsearch:
Code:
$detailsquery = "SELECT * FROM marinas WHERE country = 'UK' AND town LIKE '".$townsearch."' AND marinaname LIKE '%".$marinanamesearch."%' ORDER BY listingtype ASC, marinaname ASC";

In other SQL engines it can make a big difference. Dont know about MySQL, but is worth a try.

JR
(in need of a cool signature)

 
That's correct.

"select * from table where name = john" would try to find all the records where the value in a column called 'name' matches that in one called 'john'.

"select * from table where name = 'john'" would yield the result that you're after: find all where the name is equal to the string 'john'.


 
Cool! I solved it! YEAH!

JR
(In need of a cool signature)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top