I'm trying to improve my MySQL query.
SELECT gamename
FROM giveaway
WHERE gamename LIKE '$query'
I got an input that consists of URL's that are formed like:
- http://www.steamgifts.com/giveaway/l7Jlj/plain-sight
- http://www.steamgifts.com/giveaway/okjzc/tex-murphy-martian-memorandum
- http://www.steamgifts.com/giveaway/RqIqD/flyn
- http://www.steamgifts.com/giveaway/FzJBC/penguins-arena-sednas-world
I take the game name from the URL and use this as input for a SQL query.
- $query = "plain sight"
- $query = "tex murphy martian memorandum"
- $query = "flyn"
- $query = "penguins arena sednas world"
Now in the database the matching name sometimes has more characters like : ' !, etc.
Example:
- "Plain Sight"
- "Tex Murphy: Martian Memorandum"
- "Fly'N"
- "Penguins Arena: Sedna's World!"
So when putting in the acquired name from the URL this doesn't produce results for the 2nd, 3rd and 4th example. So what I did was use a % character.
- $query = "plain%sight"
- $query = "tex%murphy%martian%memorandum"
- $query = "flyn"
- $query = "penguins%arena%sednas%world"
This now gives result on the 1st and 2nd example.
.
On to my question:
My question is, how to better improve this so that also the 3rd and 4th ones work?
I'm thinking about adding extra % before and after each character:
- $query = "%f%l%y%n%"
- $query = "%p%e%n%g%u%i%n%s%a%r%e%n%a%s%e%d%n%a%s%w%o%r%l%d%"
But I'm not sure how that would go performance wise and if this is the best solution for it.
Is adding % a good solution?
Any other tips on how to make a good working query?
Progress:
After a bit of testing I found that adding lots of wildcards (%) is not a good idea. You will get returned unexpected results from the database, simply because you just added a lot of ways things could match.
Using the slug method seems to be the only option.