|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[SOLVED] Need query string for exact string matches
I am using the following SELECT statement.
$query = "SELECT * FROM `View1` WHERE `Topic` LIKE '%$SeeAlso%'"; The statement pulls up all strings in the db because of the % operator. Dumb question but how do I pull up exact matches even if the string is sitting next to the left or right border of a cell? I have tried removing the % operator but get no hits. In my db if I leave the % operator in the statement I get 130 hits but some hits are not exact. For example, if $SeeAlso = ed, I will get education, stuped, ed, edited, etc. How do I change the SELECT statement? |
|
#2
|
|||
|
|||
|
As long as you are using the % operator at the beginning and the end of your string to be searched then your result set will include all items that have the specified text anywhere in it.
What are you actually looking to return? |
|
#3
|
||||
|
||||
|
If $SeeAlso isn't set, you will return all fields in the View1 table...
I think you're looking to return full words? EXAMPLE: ed would return... but education/stuped/edited won't return... Use Regular Expressions: Code:
SELECT * FROM table WHERE fieldname REGEXP '[[:<:]](word)[[:>:]]' this is untested and not guaranteed to work |
|
#4
|
|||
|
|||
|
Thank you for the replies. If a user inputs the word "ed" in an input box or selects the word "ed" from a drop down list I want the user to find only those records with "ed" in the selected field. The fields to be searched are multi-word phrases so the phrase in a record may be something like "Special ed is underfunded" or "do not pick up Ed at work" or "Ed 101 is out of date" or "See the article on ed". The user selects terms to be searched or types in terms to be searched. Using the wildcard % operator I am currently selecting records that have any word with the two letters "ed" anywhere in the record including words with the letters within a word...not what I want. As I noted in my first post using the wildcard operator I am pulling up records with the words "education", "stuped", "ed", "edited", and so on when I want only "ed". If I use the = operator instead of the LIKE operator I get no records retrieved unless the field contains only the word or term selected which is also not what I want because every field to be searched contains phrases and not just one word. How do I change the SELECT statement to pick up a whole selected single words or whole selected phrases? Thank you again for the replies.
|
|
#5
|
|||
|
|||
|
MadCow, sorry I did not reply to your post. Thank you for the reply. I tried regexp but without the < and > qualifiers. I will give that a try and report back.
|
|
#6
|
|||
|
|||
|
MadCow, yu duh man.
Excellent!!!!!! Working fine. Thank you very much. |
|
#7
|
||||
|
||||
|
The regular expression i posted will also match if the string is "I love my friend ed"
If that isn't what you want... you likely want a simple equals statement Code:
SELECT * FROM table WHERE fieldname='word' |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Need query string for exact string matches |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|