The idea in that snippet is a sound one, except for the fact the search terms are hardcoded into the sql string and there are only 3 of them
You have to build the statement on the fly, like this
PHP Code:<?php
// simple search
// $query = search query from form
$query = 'hello world';
$sql = 'SELECT * FROM table WHERE ';
// split into terms on space
$terms = explode(' ', $query);
// loop through array building sql statement
for ($i=0; $i < count($terms); $i++) {
$sql = $sql."Field LIKE '%$terms[$i]%' AND ";
}
//remove final 'AND '
$sql = substr($sql, 0, -4);
// add ending SQL
$sql = $sql.'ORDER BY Field';
// do whatever with SQL string
print $sql;
?>




Reply With Quote