0

I tweaked a search script I found months ago, and am now getting back around to fixing it...

I need some help debugging it...

if(isset($_GET['search']))
{
$search = $_GET['search'];
}

$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);

$keywords = explode(" ", $search);

$keywords = array_diff($keywords, array(""));

if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM research WHERE research.keywords LIKE '%".$keywords[$i]."%' OR research.products LIKE '%".$products[$i]."%'";

}

$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {

$count = mysql_num_rows($result);
}

If I remove the 'OR ETC.' statement, It returns only the keywords. That's great, but I need it to return either keywords or products in the DB...

thanks ahead of time,
Ted

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by diafol
-2

SNIP
I need some help debugging it...

if(isset($_GET['search']))
{
$search = $_GET['search'];
}

$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);

$keywords = explode(" ", $search);

$keywords = array_diff($keywords, array(""));

if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM research WHERE research.keywords LIKE '%".$keywords[$i]."%' OR research.products LIKE '%".$products[$i]."%'";

}

$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {

$count = mysql_num_rows($result);
}

If I remove the 'OR ETC.' statement, It returns only the keywords. That's great, but I need it to return either keywords or products in the DB...

thanks ahead of time,
Ted

Edited by mike_2000_17: Fixed formatting

0

Try this:

$query = "SELECT * FROM research WHERE research.keywords LIKE '%".$keywords[$i]."%' OR research.products LIKE '%".$keywords[$i]."%'";

Your products array doesn't seem to have been set anywhere in code. I assume you want to search keywords and product fields for the individual keywords (from the form search string). BTW you don't need to include the table name when stating the fieldname if using a simple, one-table query.

$query = "SELECT * FROM research WHERE keywords LIKE '%".$keywords[$i]."%' OR products LIKE '%".$keywords[$i]."%'";

Should work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.