tags:

views:

52

answers:

3
SELECT * FROM disney;
+++++++NAME+++++++
lion king
cinderella
the little mermaid
+++++++++++++++++++

$string = "i love the movie lion king, it rocks!"

I want to get the NAME from disney where the NAME is inside the given string. It's kinda like:

'SELECT * FROM disney WHERE NAME LIKE "%'.$string.'%";'

only the other way around (but didn't work well when i tried it).

Is there a MYSQL function to check whether a data in a table cell exists in a given string?

Thanks a lot! ().i'm working with PHP!

+1  A: 

I think the following should work (I remember trying to do the same once, and I think this is what I used) :

'SELECT * FROM disney WHERE "' . $string . '" LIKE CONCAT("%",NAME,"%")';
Wookai
That will only put a wildcard on the righthand side of the query.
OMG Ponies
thank you! that indeed worked!
sombe
OMG Ponies, I've changed it to suit my needs (concat(%,name,%))
sombe
Yep, forgot left side, just edited to correct that !
Wookai
+1  A: 

There's the INSTR Function which should work for this:

SELECT * FROM disney WHERE INSTR('i love the movie lion king, it rocks!', name) > 0
Bobby
Change the double quotes to single quotes, and that will work.
OMG Ponies
@Cute Things: Thanks...not my day...
Bobby
A: 

You can also try MySQL's FULLTEXT search feature which only works with MyISAM tables:

$query = "SELECT * FROM disney WHERE MATCH(NAME) AGAINST ('$text')";
eyazici