Jump to content


Photo

Simple search problems


  • Please log in to reply
3 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 21 June 2006 - 11:08 AM

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 dog or cat 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:

<?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"); 
} 

?>


#2 teomanersan

teomanersan
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 June 2006 - 01:43 PM

$query=mysql_query("select * from ******* where opening like '%$query%' or body_text like '%$query%' LIMIT 5")


to limit the query results..

hope this helps..

good coding..

#3 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 21 June 2006 - 02:16 PM

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?

#4 teomanersan

teomanersan
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 June 2006 - 03:14 PM

$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..






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users