Mr Chris Posted June 21, 2006 Share Posted June 21, 2006 Hi Guys,Trying to build a search facility:[a href=\"http://www.slougheaz.org/greenock/search/search.php\" target=\"_blank\"]http://www.slougheaz.org/greenock/search/search.php[/a]But have a problem with it.Problem: When I hit submit without entering any data or hit submit after entering any specific term like [b]dog[/b] or [b]cat[/b] or anything it returns all the results in the database. So basically all it's doing nomatter what you enter as the searcg criteria it returns all results in my DB.Can anyone please advise – here’s the search code:[code]<?php // Connect to DB;include("***************");// Define Variable form 'Query' post;$q = isset($_GET['query']) ? trim(stripslashes(htmlspecialchars($_GET['query']))) : NULL; // Use like to match with the % wildcards;$query=mysql_query("select * from ******* where opening like '%$query%' or body_text like '%$query%'") or die(mysql_error()); while($rows = mysql_fetch_assoc($query)) { echo '<a href="http://www.slougheaz.org/test.php?newsid='. $rows['story_id'] .'">'. $rows['headline'] .'</a><br>'; echo $rows['opening'].'<br><br>'; } // If no record!!; $no_records = "Sorry - no Record Found"; if (mysql_num_rows($query) <= 0) { print ("$no_records"); } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12527-simple-search-problems/ Share on other sites More sharing options...
teomanersan Posted June 21, 2006 Share Posted June 21, 2006 $query=mysql_query("select * from ******* where opening like '%$query%' or body_text like '%$query%' [b]LIMIT 5[/b]")to limit the query results..hope this helps..good coding.. Quote Link to comment https://forums.phpfreaks.com/topic/12527-simple-search-problems/#findComment-48025 Share on other sites More sharing options...
Mr Chris Posted June 21, 2006 Author Share Posted June 21, 2006 Hi teomanersan,Thanks for that, but that's just limiting the results to 5!? It's not doing anything to ONLY return the values of the text entered in the search box.Anyone else please? Quote Link to comment https://forums.phpfreaks.com/topic/12527-simple-search-problems/#findComment-48039 Share on other sites More sharing options...
teomanersan Posted June 21, 2006 Share Posted June 21, 2006 $query=mysql_query("select * from ******* where opening like '%$query%' or body_text like '%$query%'") or die(mysql_error()); if u dont want a null character results nothing , simply add a check control if($query == ""){ echo('error');}by the way i didn`t get the idea why u used OR in the sql statement because if it will find at opening it will pass body_text..good coding.. Quote Link to comment https://forums.phpfreaks.com/topic/12527-simple-search-problems/#findComment-48078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.