2014-02-11 14:05
浏览 28


I have a table in my database with the structure something like that: ID,category,county,keyword

Now I have the category, the county and some text.

I want to take from my database the rows with the category, county that I have, BUT also the 'keyword' must be in my text

select from `posts` where `category`='$category' and `county`='$county'

Can anyone help me with the third condition ('keyword' must be in my $text)? Thank you!

图片转代码服务由CSDN问答提供 功能建议

我的数据库中有一个表格,其结构类似于: ID category county keyword



 从`posts`中选择`category` ='$ category'和`county` ='$ county'

任何人都可以帮我解决第三个问题('keyword'必须在我的 $ text 中)? 谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhun8449 2014-02-11 14:08

    If you mean the text of column keyword should be contained anywhere in the $text variable, then use

    select ID,category,county,keyword
      from `posts`
     where `category`='$category' and `county`='$county'
       and '$text' like concat('%', `keyword`, '%')

    Actually, to prevent SQL injection, it would be better to use a prepared statement using parameters for $category, $county, and $text to prevent SQL injection.

    打赏 评论

相关推荐 更多相似问题