Forum Moderators: coopster

Message Too Old, No Replies

help with MySQL Search Query

making SQL Search Query inclusive

         

mhnewmedia

9:00 am on May 20, 2005 (gmt 0)

10+ Year Member



I have got a very simple search facility to allow users to enter first part of their postcode or name of their town and returns details about their nearest office.

Search is localised to Gloucestershire in United Kingdom.

UK Postcodes are made up of 1 or 2 Letters followed by 1 or 2 numbers space followed by 1 or two numbers followed by two letters i.e. GL1 6UB, GL51 9SE etc)

At the moment it requires people to put just the first part of the Postcode (GL1 or GL51) however users being users I know that people will try and put in the whole postcode with or without the space in between the two sets.

Does anyone know of a way of allowing for this?

I am currently using the following mysql query $result = mysql_query("SELECT * FROM $table WHERE Postcode LIKE '$search' OR Town LIKE '$search'");

buriedUnderGround

10:08 am on May 20, 2005 (gmt 0)

10+ Year Member



I would put a MAXLEN="4" attribute in my input tag and do a str_replace() to remove any spaces from the posted value.
Also I would include a "%" after $search (ie; "$search%") to make sure that only values beginnign with $search were matched.

That way you should have at least 3 valid characters to match with.