Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using strpos in mysql?

I'm not sure if this is the correct function in using mysql but I was wondering if someone can help me with this problem I'm having

Ex:

I have mysql with these values

id     name
1      house_home
2      movie_film
3      restaurant_food

So if I'm trying to find movie, it should get the value movie_film.

Is strpos function the right function for this case? Or is there another way?

$string = 'movie';
$query = $db->prepare("SELECT * FROM values WHERE name = ?";
$query->execute(array($string));
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    echo $row['name']; //this should output movie_film
}

Thanks for help in advance!

like image 818
hellomello Avatar asked Nov 28 '22 17:11

hellomello


2 Answers

Using strpos you will get the position of that string and then trim the length you want

Use LOCATE('substring', 'mainstring') function

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate

like image 196
Riad Avatar answered Dec 06 '22 05:12

Riad


You can use the LIKE 'operator' in SQL.

SELECT * FROM values WHERE name LIKE '%?%'

like image 32
Marcus Recck Avatar answered Dec 06 '22 04:12

Marcus Recck