php,mysql:比较字符串 - 区分重音和不区分大小写
我需要根据一组关键字在数据库中执行名称搜索。结果应该区分重音且不区分大小写。
按照我在这里找到的解决方案加上一些修改允许不区分大小写,我使用了以下代码:
$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE _utf8 '%".strtolower($keyword)."%' COLLATE utf8_bin";
当搜索关键字包含重音符号时,这对我不起作用。但奇怪的是,这个查询在 phpMyAdmin 中运行良好(但我在某处读到这应该不可靠)。这里可能有什么问题?
更新: 我将代码修改为:
$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE LOWER(_utf8 '%$keyword%') COLLATE utf8_bin";
但我仍然没有结果。
谢谢!
i need to perform a name search in the database based on a set of keywords. the result should be accent sensitive and case insensitive.
following the solution i found here plus few modifications to allow case insensitivity, i used the following code:
$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE _utf8 '%".strtolower($keyword)."%' COLLATE utf8_bin";
this does not work for me when the search keyword contains accents. but the strange thing is that this query works well in phpMyAdmin (but i read somewhere that this shouldn't be reliable). what could be the problem here?
UPDATE:
i revised my code to:
$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE LOWER(_utf8 '%$keyword%') COLLATE utf8_bin";
but i still have no results.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试回显正在执行的 sql 并检查重音符号是否存在。
我猜他们不是...
[编辑]
你能试试这个:
php.ini
mysql:(没关系)
这个对我有用......
You can try to echo the sql being executed and check if the accents are there.
My guess they are not...
[EDIT]
Can you try this:
php.
mysql: (doesn't matter)
This one works for me...