How to use a LIKE statement to perform a search?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matt sheeyd
    New Member
    • Jan 2011
    • 7

    How to use a LIKE statement to perform a search?

    Hi,
    I am having trouble with getting rows from a MySQL table using the like statement.
    I have a table which contains information relating to 5 cities.
    eg.
    table_name
    City COLUMN_2 COLUMN_3
    Bali ... ...
    Vegas ... ...

    my sql query is currently:
    "SELECT column_2, column_3 from table_name WHERE CITY LIKE '%".$CITY."%' ";
    where $city is the input from the radio button. At the moment if the user selects just 1 city it works fine and if they select anymore than 1 it doesnt work because there is no row that contains both bali and vegas ect.
    is there any way around this, so that they can select multiple cities and it will show the rows that contain the chosen cities?

    Thanks very much.
    matt.
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    You could use OR.

    By selecting multiple cities, i take it you're looking for the inclusion of data from both cities.

    You'd first break the multi-radio button inputs into into two different variables ($city1 and $city2). Then run them in a query as shown below.

    for example:
    "SELECT column_2, column_3 from table_name WHERE CITY LIKE '%".$CITY1."% ' OR CITY LIKE '%".$CITY2."%'" ;
    niheel @ bytes

    Comment

    • matt sheeyd
      New Member
      • Jan 2011
      • 7

      #3
      thank-you very much niheel. that has solved my problem

      Comment

      Working...