Jump to content

Need help with PHP and MySql query


svgmx5

Recommended Posts

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

 

 

Link to comment
Share on other sites

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 (%).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.