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

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

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.