dreamwest Posted June 17, 2009 Share Posted June 17, 2009 I cant seem to get this to work... Im trying to use wildcards within a search string: SELECT * FROM `tags` WHERE tag like '%looking%anything%' I want to match anything either side of each word, so logically it would look like this: SELECT * FROM `tags` WHERE tag like '%looking%' And SELECT * FROM `tags` WHERE tag like '%anything%' Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/ Share on other sites More sharing options...
dreamwest Posted June 17, 2009 Author Share Posted June 17, 2009 Simple solution: $pieces = explode(" ", $search); $sql4 = "SELECT * from tags where (tag like '%".$pieces[0]."%' OR tag like '%".$pieces[1]."%')"; If someone knows a proper sql way i would love to see it... Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857846 Share on other sites More sharing options...
kickstart Posted June 17, 2009 Share Posted June 17, 2009 Hi I can see nothing wrong with your first idea (and in a quick test it works for me). Possible that one of your search strings contains something that MySQL is interpreting as a wild card? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857857 Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 SELECT * FROM `tags` WHERE tag IN('%looking%', '%anything%'); Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857860 Share on other sites More sharing options...
dreamwest Posted June 17, 2009 Author Share Posted June 17, 2009 SELECT * FROM `tags` WHERE tag IN('%looking%', '%anything%'); Thanks. But is there a way to search each word individually without using explode to separate the string The only solution i can think of is to strreplace the spaces and set it as a variable $search = "looking anything" $search = str_replace( " ", "%', '%", $search ); SELECT * FROM `tags` WHERE tag IN('%$search%'); Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857892 Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 But is there a way to search each word individually without using explode to separate the string What's wrong with explode? Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857894 Share on other sites More sharing options...
kickstart Posted June 17, 2009 Share Posted June 17, 2009 SELECT * FROM `tags` WHERE tag IN('%looking%', '%anything%'); Don't think that would work, as that is looking for a string equal to '%looking%' or '%anything%', rather that doing a LIKE on each. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857898 Share on other sites More sharing options...
dreamwest Posted June 17, 2009 Author Share Posted June 17, 2009 But is there a way to search each word individually without using explode to separate the string What's wrong with explode? It might have more or less than 2 words in the string So if the string is 1 word, this code will search for tags with a zero value because $pieces[1] is nothing : $sql4 = "SELECT * from tags where (tag like '%".$pieces[0]."%' OR tag like '%".$pieces[1]."%')"; Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857901 Share on other sites More sharing options...
kickstart Posted June 17, 2009 Share Posted June 17, 2009 Hi You could use explode and then implode (although you could also do it using a regular expression). $pieces = explode(" ", $search); $pieceStr = implode("%' OR tag like '%", $search); $sql4 = "SELECT * from tags where (tag like '%$pieceStr%')"; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857905 Share on other sites More sharing options...
dreamwest Posted June 17, 2009 Author Share Posted June 17, 2009 Hi You could use explode and then implode (although you could also do it using a regular expression). $pieces = explode(" ", $search); $pieceStr = implode("%' OR tag like '%", $search); $sql4 = "SELECT * from tags where (tag like '%$pieceStr%')"; All the best Keith Excellent. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/162530-solved-wildcards-with-a-string/#findComment-857908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.