Jump to content


Photo

Wildcard Query


  • Please log in to reply
6 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 10 September 2006 - 01:52 PM

Hi all

I am trying to perform a query on a field on my sql database called pcode, basically the field holds the data for a postcode, so for example, the rows might look like

W1H 7YH
ST64 2HG
N12 2ES

I want to perform a query on the postcode field allowing rows to be found which contain any kind of match, so it ignores spaces placed in the search box or in the field.

For example, if someone does a search on "W1H7HG" then it would return the rows that start or contain the whole postcode "W1H 7HG",

again, if they did a search for "S E2 2 7Y H" then it would return the rows that start or contain the whole postcode "SE22 7YH",

and again if they did a search for "E H 4" then it would return the rows that start or contain the whole postcode "EH4" in the field

I hope that makes some sense, im basically trying to get around people putting extra spaces into the search or missing a space, I normally use the code

pcode LIKE '%".$_GET['pcode']."%'

But it seems to NOT ignore additional spaces.

Is there a wildcard search to do what I want?

Thanks in advance

Ed

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 September 2006 - 02:09 PM

Because the British establishment seems incapable of coming up with standard structures for code (be it phone area codes or postcodes) I store postcode fields twice in my postcode table, formatted and with no spaces for searching

[pre]| W1H 7YH  | W1H7YH  |
| ST64 2HG  | ST642HG  |
| N12 2ES  | N122ES  |[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 10 September 2006 - 02:54 PM

no easy way around it then?

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 September 2006 - 04:05 PM

Because the British establishment seems incapable of coming up with standard structures for code (be it phone area codes or postcodes) I store postcode fields twice in my postcode table, formatted and with no spaces for searching

[pre]| W1H 7YH  | W1H7YH  |
| ST64 2HG  | ST642HG  |
| N12 2ES  | N122ES  |[/pre]


Wouldn't it be easier to do
str_replace(' ','',$string)
on them?

#5 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 10 September 2006 - 05:00 PM

It would be, but the postcodes in the sql database have spaces in them, unless you can put

str_replace(' ','',$string)

into the query, so it views the data in the sql database without spaces and does a search, is that possible? I imagine it's not as simple as

$temp = str_replace(" ", "", postcode);
$sql = "SELECT * FROM users WHERE $temp LIKE '%".$_GET['postcode']."%'";

Then I could remove the spaces of the forms posted field and get a match

Can this be done?

Thanks in advance

Ed

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 September 2006 - 05:28 PM

I'm useless at regex, but if there's a pattern to strip out spaces from a string you could have something like this (not sure of exact syntax for regex() )

.... WHERE REGEX(pcode, pattern) LIKE '%$search%'
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 September 2006 - 10:53 PM

Just checked the MySQL manual - there is a str_replace equivalent

... WHERE REPLACE(pcode, ' ', '') LIKE '%$search%'
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users