JasonDFR
05-07-2008, 01:57 PM
I am allowing users to search a mysql table. I want the user's search term to return rows that contain the search term in any of 5 different columns (city, departnum, depart, region, postal). I have made 5 different SELECT statements, moving to the next if a result isn't found. The following code works, but there must be a simpler way, I just can not figure it out.
BTW, I am teaching myself php and mysql. I am new. Thanks for the help!
$query = "SELECT * FROM marches WHERE city = \"$search\" ORDER BY city";
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE departnum = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE depart = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE region = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE postal = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
?>
Thanks so much. As always, these forums are great. I have learned so much from them already.
Jason
BTW, I am teaching myself php and mysql. I am new. Thanks for the help!
$query = "SELECT * FROM marches WHERE city = \"$search\" ORDER BY city";
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE departnum = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE depart = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE region = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
if ($numrows == 0)
{
$query = "SELECT * FROM marches WHERE postal = \"$trimmed\" ORDER BY city";
}
// Results
$numberresults=mysql_query($query);
$numberrows=mysql_num_rows($nresults);
?>
Thanks so much. As always, these forums are great. I have learned so much from them already.
Jason