Jump to content

Search function


graham23s

Recommended Posts

Hi Guys,

 

in my search if i do 1 word say: graham it searches and find it ok but if there is characters in between them say

 

graham-another word.another word the characters after the work seem to bring back no results

 

basically i just need to search the word graham to get results the minute i put 2 or more words together the reults bring back nothing

 

is there a way to be able to search multiple words?

 

code: part of the query

 

$search_query .= "WHERE (`file_name` LIKE '%$keywords%' OR `file_name` LIKE '%$keywords%')";

 

cheers

 

Graham

 

 

Link to comment
https://forums.phpfreaks.com/topic/65102-search-function/
Share on other sites

I would do:

(index.php?search=Search%20Function)

$keywords=$_GET['search'];
$keywords=trim($keywords);
$result=mysql_query("SELECT * FROM $table WHERE words LIKE '$keywords');
$num=mysql_numrows($result);
echo "Results: $num";
while($results=mysql_fetch_array($result))
{
  print "<tr class='alt'><td><a href='URL'>$results[FIELD]</a></td></tr>";
}

 

etc... I'm not very good at PHP so i'm probably wrong, but hope it helps :D

Link to comment
https://forums.phpfreaks.com/topic/65102-search-function/#findComment-324918
Share on other sites

Split the keyword string into a word array by using PHP's strtok() or explode(). Then like so:

<?php
// Say the words are in $keyword_array...
$search_query = "WHERE (";
for($i=0;$i<count($keyword_array)-1;$i++) $search_query .= "'file_name' LIKE '%".$keyword_array[$i]."%' ";
$search_query .= "'file_name' LIKE '%".$keyword_array[count($keyword_array)-1]."%')";
?>

Hope that helps.

 

Note: The code in the post above is BAD for security!!!

Link to comment
https://forums.phpfreaks.com/topic/65102-search-function/#findComment-324923
Share on other sites

Archived

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

×
×
  • 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.