svgmx5 Posted March 4, 2011 Share Posted March 4, 2011 I have this query that searches the database based on what the user inputed. However i'm having the following issues with it: The query is supposed to look for the name of a city in a database table that stores the city names along with their state and country CITY STATE COUNTRY Now i have an input field where the user can search a location and it searches the location from the database. When they search the database the input is in the following format: city, state, country I then use the following script to separate them into three fields $split = explode(',', $location); // $location being the city, state, country $city = $split[0]; $state = $split[1]; $country = $split[2]; Then i use this MySql Query to search the fields to make sure the city and country match $get_location = mysql_query("SELECT * FROM locations WHERE name LIKE '$city' AND country LIKE '$country'") or die(mysql_error()); $tmp_loc = mysql_fetch_assoc($get_location); This is where the problem beings...if i use "AND" to search both fields i get no results even though there are results in there however if i change it to an "OR" statement it finds teh locations however it doesn't do an accurate search... for example someone searches for Toledo, Ohio, United States, the user will get Toledo, Spain instead of the right Toledo, since Toled, Spain is at the top of the table I've been trying to get this but i can't get this to work at all every time i use AND it gives me no results I hope someone here can check help me out Quote Link to comment https://forums.phpfreaks.com/topic/229572-need-help-with-php-and-mysql-query/ Share on other sites More sharing options...
void Posted March 4, 2011 Share Posted March 4, 2011 well i suspect there's whitespaces left after you use explode(). try $city = trim($split[0]); $state = trim($split[1]); $country = trim($split[2]); and why would you use 'LIKE'? it's useless if you don't use wildcard (%). Quote Link to comment https://forums.phpfreaks.com/topic/229572-need-help-with-php-and-mysql-query/#findComment-1182786 Share on other sites More sharing options...
svgmx5 Posted March 4, 2011 Author Share Posted March 4, 2011 Void Thanks, i guess i did have extra white space in there Also yea i know i'm supposed to use '%' i just always forget to add it.. thanks for the reminder though Quote Link to comment https://forums.phpfreaks.com/topic/229572-need-help-with-php-and-mysql-query/#findComment-1182787 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.