CodeMama Posted July 21, 2009 Share Posted July 21, 2009 I can display all the results from the two tables but my search isn't working so I can't get it to say display all the "A" records.... // Run query on submitted values. Store results in $SESSION and redirect to restaurants.php $sql = "SELECT name, address, inDate, inType, notes, critical, cviolations, noncritical FROM restaurants, inspections WHERE restaurants.name <> '' AND restaurant.ID = inspection.ID"; if ($searchName) { $sql .= "AND restaurants.name LIKE '%". mysql_real_escape_string($name) ."%' "; if(count($name2) == 1) { $sql .= "AND restaurants.name LIKE '%". mysql_real_escape_string($name2[1]) ."%' "; } else { foreach($name2 as $namePart) { $sql .= "AND restaurants.name LIKE '%". mysql_real_escape_string($namePart) ."%' "; } } } if ($searchAddress) { $sql .= "AND restaurants.address LIKE '%". mysql_real_escape_string($address) ."%' "; } $sql .= "ORDER BY restaurants.name;"; $result = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/166810-solved-search-query-to-two-tables-not-working/ Share on other sites More sharing options...
kickstart Posted July 21, 2009 Share Posted July 21, 2009 Hi Think this:- if(count($name2) == 1) { $sql .= "AND restaurants.name LIKE '%". mysql_real_escape_string($name2[1]) ."%' "; } should be:- if(count($name2) == 1) { $sql .= "AND restaurants.name LIKE '%". mysql_real_escape_string($name2[0]) ."%' "; } First occurance of an array is 0 not 1. Also when you loop round you probably want OR instead of AND for each check on restaurants.name All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/166810-solved-search-query-to-two-tables-not-working/#findComment-879652 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 if that doesn't work, echo $sql, and figure out where the statement differs from what you expect also, @CodeMama, you should know to use mysql_error() by now $result = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/166810-solved-search-query-to-two-tables-not-working/#findComment-879657 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.