Hi people sorry to bother you, I'm kinda new to php and I'm struggling to edit this code so that is searches for multiple words.
I understand you need to use the something along the lines of this
$sql = "SELECT * FROM myTable WHERE ";
// clean up your search first
$search = trim($_POST['search']);
$search = strip_tags($search);
$search = mysql_real_escape_string($search);
preg_match_all('|(")(["]+)\1|', $search, $groups, PREG_PATTERN_ORDER);
preg_match_all('|\b([a-z\d_-]+)\b|i', $search, $words, PREG_PATTERN_ORDER);
$like = array();
if (count($groups) > 0) {
foreach ($groups[2] as $x) {
$like[] = "searchCol LIKE \"%$x%\"";
}
}
if (count($words) > 0) {
foreach ($words[1] as $x) {
$like[] = "searchCol LIKE \"%$x%\"";
}
}
$sql .= '(' . implode(') OR (', $like) . ')';
// Now $sql holds your query string
?>
i simply keep getting errors when im trying to insert the above code into the one below. please can some hep with the edit of these code. Thanks
<?
print "<html><head><title>My Search Engine</title></head><body>n";
if( $_POST['keyword'] )
{
include("dbconnect.php");
/ Get timestamp before executing the query: /
$start_time = getmicrotime();
/* Set $keyword and $results, and use addslashes() to
- minimize the risk of executing unwanted SQL commands: */
$keyword = addslashes( $_POST['keyword'] );
$results = addslashes( $_POST['results'] );
/ Execute the query that performs the actual search in the DB: /
$result = mysql_query(" SELECT p.page_url AS url,
COUNT(*) AS occurrences
FROM page p, word w, occurrence o
WHERE p.page_id = o.page_id AND
w.word_id = o.word_id AND
w.word_word = "$keyword"
GROUP BY p.page_id
ORDER BY occurrences DESC
LIMIT $results" );
/ Get timestamp when the query is finished: /
$end_time = getmicrotime();
$number = mysql_num_rows($result);
if ($number != "0") {
/ Present the search-results: /
print "<h2>Search results for '".$_POST['keyword']."':</h2>n";
for( $i = 1; $row = mysql_fetch_array($result); $i++ )
{
print "$i. <a href='".$row['url']."'>".$row['url']."[/url]n";
print "(Index count: ".$row['occurrences'].")
n";
}
} else {
print "<p>No matching results found/p>";
}
/ Present how long it took the execute the query: /
print "query executed in ".(substr($end_time-$start_time,0,5))." seconds.";
}
else
{
/ If no keyword is defined, present the search page instead: /
}
print "</body></html>n";
/ Simple function for retrieving the current timestamp in microseconds: /
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
?>