Creating a search engine for my articles

cyberphant0m

Member
Oct 21, 2003
99
0
0
I've got a bunch of articles in my DB and I want to be able to search them for terms using the AND or OR operand depending on a radio button. Right now im working on just getting the AND part of things working and here's what i've got:
The query sent in is in the text box named "query" (duh)... My connection to MySQL server is done in an external file and the DB is selected... everytime I try to search the DB nothing is returned although there are articles in there that should be returned...

$query_words[] = split(" ", $query); //put each word in a seperate element
$query_words2 = join($query_words, "%"); //insert wildcards into the query
$query = "%$query_words2%"; // put wildcards before and after first/last words
$sql = "SELECT id, title, author, section, LEFT(body, 47) as summary FROM articles WHERE BODY LIKE '$QUERY'";
$result = mysql_query($sql); //send the query
while ($row = mysql_fetch_row($result)) //print out each result in it's own table...
{
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="summarytitle">
<tr>
<td bgcolor="#DEDFCE" class="underline"><font size="2" face="Tahoma">&raquo; <a href="<?=$row[3]?>.php?i=<?=$row[0]?>"><?=$row[1]?> by <?=$row[2]?></a>

<?=$row[4]?></font></td>
</tr>
</table>
<?php
<?php
}
mysql_free_result($result); //clean it up
?>

Sorry that this post is kinda long... Thanx for any help provided...
 

BZ

Member
Jan 9, 2003
160
0
0
I think you need a % after $QUERY
that's the wildcard symbol.

also to debug do an echo on the sql statement that is generated by the script.
 

BZ

Member
Jan 9, 2003
160
0
0
also maybe that's just a different sytax than I use but don't you have to break out of the string to evaluate the variable?

$sql = "SELECT id, title, author, section, LEFT(body, 47) as summary FROM articles WHERE BODY LIKE '".$QUERY."%'";

edit: I'm wrong about that - works either way
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Not sure if you realize this but your queries are going to be incredibly slow.

Having a wildcard before the string you are looking for in the body prevents it from using an index for this search.
If you want the search to run a reasonable speed you need a separate table where each word that occurs in an article is in its own row.
 

cyberphant0m

Member
Oct 21, 2003
99
0
0
Thanx for the echo advice. Turns out that instead of BODY LIKE '$query', I had BODY LIKE '$QUERY' (vars are case-sensitive)... Thanx for your help guys...