mattm1712 Posted October 9, 2012 Share Posted October 9, 2012 hi i am trying to return values in my data base which are like what my varibale is say my data base has these values is 1 Manchester Utd 2 Chester 3 Macclessfiled 4 Liverpool and i have a variable $tl = "chester city"; and i wanted to echo out the values that look like 'chester city' eg i want a list that has chester in it $tl = "chester city"; $query2 = mysql_query("SELECT * FROM teams LIKE '{$tl}'"); while($array2 = mysql_fetch_assoc($query2)) { echo $array2['name']; } why whould this not work? cheers matt Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 9, 2012 Share Posted October 9, 2012 I've moved this to MySQL. The simple answer is that LIKE works using wildcards, and can only find matches where the search term is within the result. So LIKE('%Chester%') would match 'Chester City', but LIKE('%Chester City%') will not match 'Chester'. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 LIKE is an operator that allows for wild cards. You can find anything LIKE '%a%', which is anything with a lowercase A in it. It's not a magical "read this and figure out what's similar" function. You have "chester city." Even if you split that into WHERE field LIKE '%chester%' AND field LIKE '%city%' you'd still get Man U in there. Also, your query is fundamentally wrong, you can't just FROM table LIKE string Quote Link to comment Share on other sites More sharing options...
Beeeeney Posted October 9, 2012 Share Posted October 9, 2012 hi i am trying to return values in my data base which are like what my varibale is say my data base has these values is 1 Manchester Utd 2 Chester 3 Macclessfiled 4 Liverpool and i have a variable $tl = "chester city"; and i wanted to echo out the values that look like 'chester city' eg i want a list that has chester in it $tl = "chester city"; $query2 = mysql_query("SELECT * FROM teams LIKE '{$tl}'"); while($array2 = mysql_fetch_assoc($query2)) { echo $array2['name']; } why whould this not work? cheers matt Now don't quote me on this because I'm a beginner but that's not going to echo anything because you're trying to call something which doesn't exist. If in your DB you has "Chester City" and you queried something LIKE "Chester%", it would return "Chester City", but not the other way around. I'm sure one of the experts on here will correct me if I'm wrong. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 Example mysql> SELECT * FROM teams; +----+-----------------------------+ | id | team_name | +----+-----------------------------+ | 1 | History of Chester FA Clubs | | 2 | Chester | | 3 | Chester City | | 4 | chester United | | 5 | manchester united | +----+-----------------------------+ 5 rows in set (0.00 sec) mysql> SELECT * FROM teams -> WHERE team_name LIKE '%chester%'; +----+-----------------------------+ | id | team_name | +----+-----------------------------+ | 1 | History of Chester FA Clubs | | 2 | Chester | | 3 | Chester City | | 4 | chester United | | 5 | manchester united | +----+-----------------------------+ 5 rows in set (0.00 sec) Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 9, 2012 Share Posted October 9, 2012 Barand, I'm a little confused what your example is supposed to show? Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 Barand was proving that even if you used LIKE properly, it would still return both terms with "chester" in them, not just "chester city" Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 9, 2012 Share Posted October 9, 2012 Right. I guess I was just confused because his first query returns the same results because he doesn't have any without 'chester' in them? *shrug* Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 And I was also showing how the query should be written Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 The inconsistent spacing in [ code ] tags is starting to make me twitch. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 Annoys the hell out of me. Impossible to post ASCII artwork Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 9, 2012 Share Posted October 9, 2012 Annoys the hell out of me. Impossible to post ASCII artwork I first noticed it when I tried to post a "picture" of my dog. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 Got a couple of labradors myself. I look forward to seeing that when they fix the board. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 It was here. I fixed it for the new poorly spaced forum. Quote Link to comment 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.