项目中有这样一个需求,或者说可能,就是用户会输入多个空格作为自己的密码,而当保存到MYSQL中时,MYSQL会当作一个空格进行存储,这样在搜索的时候,无论输入几个空格作为搜索条件,结果都一样,请问有什么办法可以解决吗?或者说对于MYSQL,这个问题无解?

解决方案 »

  1.   

    谁说的。这两个结果就不一样。select password('      '),password('  ');结果
    *89F79AE862AF0A89753E2C97CF2B628B28C5B564        *5C989A26E8BA4421E925935D8624BC0629F699D8
      

  2.   

    恩恩,利用password函数是个好办法,但是他原有的数据库设计中,是直接把密码不经过转换存进去的,所以要改就改动大了,有其他方法吗?
      

  3.   

    那你就传之前判断他的长度。
    具体看
    lpad函数。
      

  4.   

    还有个问题是,如果使用这个函数的话,搜索的时候就可能没办法了
    SELECT * FROM t0 WHERE C1 = password(' ');查不出结果~
      

  5.   

    给你个例子,不知道你是怎么测试的。mysql> create table space_save(
        -> id int not null AUTO_INCREMENT PRIMARY key,
        -> space_field varchar(255) not null
        -> )engine=myisam;
    Query OK, 0 rows affected (0.03 sec)mysql>
    mysql> insert into space_save(space_field) values
        -> (password(' ')),
        -> (password('  ')),
        -> (password('   ')),
        -> (password('    '));
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0mysql>
    mysql> select * from space_save where space_field = password(' ');
    +----+-------------------------------------------+
    | id | space_field                               |
    +----+-------------------------------------------+
    |  1 | *1A256E4E2FE95B8BF7349C168991EA8035D1359B |
    +----+-------------------------------------------+
    1 row in set (0.00 sec)mysql> select * from space_save where space_field = password('  ');
    +----+-------------------------------------------+
    | id | space_field                               |
    +----+-------------------------------------------+
    |  2 | *5C989A26E8BA4421E925935D8624BC0629F699D8 |
    +----+-------------------------------------------+
    1 row in set (0.00 sec)mysql> select * from space_save where space_field = password('   ');
    +----+-------------------------------------------+
    | id | space_field                               |
    +----+-------------------------------------------+
    |  3 | *CB79A6814789720143FACC8A1FBD2347193BCBF4 |
    +----+-------------------------------------------+
    1 row in set (0.00 sec)mysql> select * from space_save where space_field = password('    ');
    +----+-------------------------------------------+
    | id | space_field                               |
    +----+-------------------------------------------+
    |  4 | *D91B68729F5831028CF9B6CB5D0340F9BA69D643 |
    +----+-------------------------------------------+
    1 row in set (0.00 sec)mysql>
      

  6.   

    OK~查到原因了,数据库设计的时候密码字段的长度是20,所以把PASSWORD函数转化的密码给截断了,所以无法再去匹配了,万分感谢你的大力帮助,现在结帖给分~~