开发者

two words and blank spaces not working in MYSQL query using LIKE

i'm making a searcher for my site and this works fine with one word querys

 $consulta = mysql_query("SELECT * FROM recetas WHERE (titulo LIKE '%$busqueda%' OR intro LIKE '%$busqueda%' OR extra LIKE '%$busqueda%') ORDER BY id DESC");

But if i type 'two words' it doesn't give me result, $busqueda it's result from a <input/> given开发者_StackOverflow through $_POST['TERM']

any idea what i'm missing?

SOLVED

i was missing to encode the variable to URI... oops haha THANKS!


Think of how your query will look at the end:

Select ... where '%two words%. ...

If you want to search for words like that, you'll have to massage the data to look more like:

 ... Like '%two%words%'
 ... Like '%two%' or like '%words%'

depending on your exact search requirements


Unless the two words are adjacent in the text, the LIKE operator won't find them. You may want to use full text search.

In order to find two non-contiguous words, the input would need to be split up into two separate values and the query would have to look something like this:

WHERE (titulo LIKE '%$word1%' OR intro LIKE '%$word1%' OR extra LIKE '%$word1%' OR
       titulo LIKE '%$word2%' OR intro LIKE '%$word2%' OR extra LIKE '%$word2%' )

That is assuming you want a match with either word. If both must match, then something like this:

WHERE (titulo LIKE '%$word1%' AND titulo like '%$word2%' OR
       intro LIKE '%$word1%' AND intro LIKE '%$word2%'  OR 
       extra LIKE '%$word1%' AND extra LIKE '%$word2%' )

And one other thing. It would be better to use parameterized queries to avoid an SQL injection.


where ( MATCH( titulo, intro, extra) AGAINST ('word1 word2' in BOOLEAN MODE))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜