Jump to content

Simple search problems


Mr Chris

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]
Link to comment
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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.