I have a simple MYSQL database that I’m trying to search, I have four fields
waterway, embark, disembark, region, in a search form. I can get the php to return search results for one field, but when I add a second it just lists the whole database. Each field is named different in the form, I’ve used AND and OR in my MYSQL SELECT statement, but it doesn’t seem to work when I try to search with more than one field.
$query = mysql_query("SELECT * FROM members WHERE river LIKE '%$waterway%' OR canal LIKE '%$waterway%' OR lakeloch LIKE '%$waterway%' OR sea LIKE '%$waterway%' LIMIT 0, 50");
while ($row = @mysql_fetch_array($query))
{
that’s because you don’t have a field named pickup. same goes for your other like’s (no fields named river, canal, lakeloch, or sea). looks to me like you have the LIKE statement backwards in your head. it goes like this…
field LIKE variable
waterway LIKE ‘%river%’
The search field waterway searches ‘river’ ‘canal’ etc fine, that’s the bit that works. It’s when I try to get my second field in my search form to work with the first.
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?
//error message (not found message)begins
$XX = "No Record Found, please search again";
//query details table begins
$query = mysql_query("SELECT * FROM members WHERE river LIKE '%$waterway%' OR canal LIKE '%$waterway%' OR lakeloch LIKE '%$waterway%' OR sea LIKE '%$waterway%' LIMIT 0, 50");
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["memberID"];
$variable2=$row["name"];
$variable3=$row["tel"];
$variable4=$row["email"];
//table layout for results
print ("<tr>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td><a href= $variable4>$variable4</a></td>");
print ("</tr>");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
?>
$query = "SELECT *, MATCH(`river` ,`canal` ,`river` ,`lakeloch` ,`sea`) AGAINST ('".$_POST['inputfield']."') AS score
FROM `members`
WHERE MATCH(`river` ,`canal` ,`river` ,`lakeloch` ,`sea`) AGAINST ('".$_POST['inputfield']."' IN BOOLEAN MODE)
AND ((MATCH(`river` ,`canal` ,`river` ,`lakeloch` ,`sea`) AGAINST ('".$_POST['inputfield']."' IN BOOLEAN MODE)) > 0)
ORDER BY SCORE DESC";