Jump to content

Archived

This topic is now archived and is closed to further replies.

Mr Chris

Simple search problems

Recommended Posts

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]

Share this post


Link to post
Share on other sites
$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..

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
$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..

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.