mysql like难题

fkueso 2009-06-09 04:59:22
select * from product where model like '%$key%'
这是我写的一条查询语句,当我$key='a' 的时候,查询怎么会中文的查询结果,只要包含‘工’字的结果都会显示出来!
然后我把语句改成这样:
$key = strtoupper($key);
select * from product where binary upper(model) like '%$key%'
然后再将$key='a'这样查询的中文不会显示出来,但像'sdf&fsd'这样的也会被显示出来!
我用的是UTF8,我觉得和编码的关系可能不大,我把查询的字串转换成二进制对比过,都是一样!
百思不得其解,望高人指点迷津!万分感谢!
...全文
53 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fkueso 2009-06-11
  • 打赏
  • 举报
回复
最后结论还是编码的原因,ACMAIN_CHM厉害!~
fkueso 2009-06-10
  • 打赏
  • 举报
回复
呵呵,兄弟可能没有仔细看我的贴子!如果加binary能解决,我还用发贴啊!~你可以看一下五楼我的问题!~
wxq4100798 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 fkueso 的回复:]
谢谢11楼的兄弟,如果正如你的那样,有没有什么解决办法!
[/Quote]
哈哈,结贴,将分全给我,嘿嘿
这是mysql 解决方法不是给你了嘛,加 binary 关键字
fkueso 2009-06-10
  • 打赏
  • 举报
回复
谢谢11楼的兄弟,如果正如你的那样,有没有什么解决办法!
fkueso 2009-06-10
  • 打赏
  • 举报
回复
show create table product:
product CREATE TABLE `product` ( `pid` smallint(5) unsigned NOT NULL
`pmodel` varchar(20) CHARACTER SET utf8 NOT NULL
`bigclass` smallint(2) unsigned NOT NULL
`smallclass` smallint(2) unsigned NOT NULL
`pindex` text COLLATE utf8_unicode_ci NOT NULL
`pdescription` text COLLATE utf8_unicode_ci NOT NULL
`pimage` char(18) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`pid`)
KEY `bigclass` (`bigclass`)
KEY `smallclass` (`smallclass`)
CONSTRAINT `product_ibfk_1` FOREIGN KEY (`bigclass`) REFERENCES `class` (`cid`)
CONSTRAINT `product_ibfk_2` FOREIGN KEY (`smallclass`) REFERENCES `class` (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
zgycsmb 2009-06-10
  • 打赏
  • 举报
回复
再看看show create table product,字段的COLLATION设置。
wxq4100798 2009-06-10
  • 打赏
  • 举报
回复
select * from product where model like '%$key%'
改成
select * from product where binary model like '%$key%'

mysql 自身的问题
fkueso 2009-06-09
  • 打赏
  • 举报
回复
字符集的问题应该怎么解决?
show create table product这个执行出错!
ACMAIN_CHM 2009-06-09
  • 打赏
  • 举报
回复

正是你的字符集的原因!
microant 2009-06-09
  • 打赏
  • 举报
回复
show create table product;的结果呢?
最好再帖几条数据出来看看。
fkueso 2009-06-09
  • 打赏
  • 举报
回复
显示结果和楼上的一样!
microant 2009-06-09
  • 打赏
  • 举报
回复
mysql> show variables like 'c%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
| completion_type | 0 |
| concurrent_insert | 1 |
| connect_timeout | 5 |
+--------------------------+----------------------------+

再看看show create table product,字段的COLLATION设置。
fkueso 2009-06-09
  • 打赏
  • 举报
回复
select * from product where model like '%$key%',这样搜索中文就没问题
select * from product where binary model like '%$key%'这样就搜不到了,我刚开始也认为是编码的问题,但
我用mb_detect_encoding()检测都是utf8,而且二进制我也比对过,都一样!
microant 2009-06-09
  • 打赏
  • 举报
回复
set names utf8;
确认页面和数据表的charset也都是utf8。

show create table table_name确认一下。
tfxg 2009-06-09
  • 打赏
  • 举报
回复
覺得與編碼有關
程序猿之殇 2009-06-09
  • 打赏
  • 举报
回复
将表结构,及相关的数据帖出来,看看.
fkueso 2009-06-09
  • 打赏
  • 举报
回复
补充model字段是varchar();

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧