php 在mysql中使用strpos?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11060976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:40:59  来源:igfitidea点击:

using strpos in mysql?

phpmysqldatabasestringfunction

提问by hellomello

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

我不确定这是否是使用 mysql 的正确功能,但我想知道是否有人可以帮助我解决我遇到的这个问题

Ex:

前任:

I have mysql with these values

我有这些值的 mysql

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.

因此,如果我试图找到movie,它应该获得值 movie_film。

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

strpos 函数是这种情况下的正确函数吗?或者还有其他方法吗?

$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!

提前感谢您的帮助!

回答by Riad

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

使用 strpos 您将获得该字符串的位置,然后修剪您想要的长度

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

使用LOCATE('substring', 'mainstring')功能

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

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

回答by Marcus Recck

You can use the LIKE'operator' in SQL.

您可以LIKE在 SQL 中使用“运算符”。

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

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

回答by Er. Anurag Jain

Please try LIKE operatorfor it

请尝试LIKE operator一下

SELECT * from values where name LIKE '%?%';