using strpos in mysql?

33,037

Solution 1

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

Solution 2

You can use the LIKE 'operator' in SQL.

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

Solution 3

Please try LIKE operator for it

SELECT * from values where name LIKE '%?%';
Share:
33,037
hellomello
Author by

hellomello

On the path to learning all things React Native at the moment. I'll be right back!

Updated on August 22, 2020

Comments

  • hellomello
    hellomello over 3 years

    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!

  • user151841
    user151841 about 9 years
    To trim, the SQL TRIM() function will not do the job as it only trims specified characters: dev.mysql.com/doc/refman/5.0/en/… Instead what you need is SUBSTRING(): dev.mysql.com/doc/refman/5.0/en/…