doufangzhang4454
2016-10-04 09:00
浏览 88
已采纳

MySQL SELECT * FROM表WHERE名称LIKE(?)OR owner LIKE(?); [重复]

This question already has an answer here:

As you can see in the title, I have a table that I want to take use of in a website I have.

This is how the MySQL statement looks like now: SELECT * FROM bth_nav WHERE name LIKE (?) OR owner LIKE (?);

On my website I want to either search for name or owner. Right now I can only search for name but not owner. This is why I'm asking for help, I've tried to rewrite the statement but however I do it i can only search for one of them.

Thanks in advance.

</div>

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

此问题已经存在 这里有一个答案:

  • PHP PDO准备语句 - MySQL LIKE查询 3 answers

    正如您在标题中看到的,我有一个我想要使用的表格 我有一个网站。

    这就是现在的MySQL语句: SELECT * FROM bth_nav WHERE name LIKE(?)OR owner LIKE(?);

    在我的网站上,我想搜索姓名或所有者。 现在我只能搜索姓名但不能搜索所有者。 这就是为什么我要求帮助,我试图重写声明但是我这样做只能搜索其中一个。

    提前致谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douweicheng5532 2016-10-04 09:07
    已采纳

    The answer will be a little more complicated then you would expect.

    I see question marks in your query, this way I can assume you're using prepared statements (which is good!).
    Your old statement was using 1 value (for the name), and now you want to use 2.

    You have some options for this to work.

    The easiest one would be that you bind the value twice, but that's not the nicest way.

    A better way would be to name your parameters, like this:

    SELECT * FROM bth_nav WHERE name LIKE (:nameOwner) OR owner LIKE (:nameOwner);
    

    Hence that I use the same name for both parameters here. Now you can bind the value to the named parameter with bindValue, and you need do this only once.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题