I don’t know how to solve this situation. The problem is with the OR operator that I’m using to retrieve the location from zip_codes table. For example If somebody enters just “NY” as the input I want to retrieve just this field.
The table data is like this:
+------+-------+--------------------------------------+
| zip | state | city |
+------+-------+--------------------------------------+
| 12345| NY | Schenectady |
+------+-------+--------------------------------------+
And for example possible input is “NY”, so my query looks like:
SELECT zip, state, city
FROM zip_codes
WHERE name = 'NY'
OR zip = 'NY'
OR city ='NY'
LIMIT 1
I want to retrieve just the field state in the above case, because this means the user sets just the state as the reference. If they enters “Schenectady” – city field, if they enters “12345” – just the zip code field, etc..
Yes, I can set this in PHP from a bunch of if’s but maybe is some SQL query outhere that I don’t know
1 Answer