douya2982 2016-11-04 10:10
浏览 161

PHP检查mysql数据库是否包含字符串中的字符串

So I have a database column where it has like past usernames.

This is what the row would look like if it had info.

bill, joe, ryan

What I am trying to do is check ever single row and check if like any of them contain joe. I have one possible theory on how to do it but what I fear is say someones past username is this:

billjoe

Im afraid it would think it exists in database because of that bill in it i would want it to ignore it if its not exactly bill.

Whats confusing is it could have for example 3 names in 1 string seperated by commas.

My theory on how to do it is like this:

$stmt = $db->prepare("SELECT * FROM users WHERE pastusernames LIKE :pastusernames");
$stmt->execute(array(':pastusernames' => "%bill%"));

But that may even be totally wrong. So is there anyway to do this correctly?

  • 写回答

2条回答 默认 最新

  • drsqpko5286 2016-11-04 10:20
    关注

    Not sure how write in PHP. But the sql should look like this:

    SELECT * 
    FROM users 
    WHERE ', ' + pastusernames + ',' LIKE '%, bill,%'
    

    adding the , you make sure the match the whole thing and not just a part

    评论
  • doutong7216 2016-11-04 10:48
    关注

    I am not sure but may be below query may be work for you

    $stmt = $db->prepare("SELECT * FROM users WHERE pastusernames LIKE :pastusernames");
    $stmt->execute(array(':pastusernames' => "bill"));
    
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部