dongwen5019 2016-11-10 09:36
浏览 87
已采纳

这是MySQL中的一个错误吗?

I am using mysql and php (Laravel) and facing a very strange situation .

I am trying get an encrypted user type and user id and decrypt it . and then find the records for that user id .

my code is like this . i am using Laravel framework .

        $key = Config::get('app.key');
        $decodedUserIDwithType = base64_decode($encryptedUserIDwithType);
        $decryptedUserIDwithType = mcrypt_decrypt(XXXXX , $key, $decodedUserIDwithType, XXXXXX);
        $userIDwithType = $decryptedUserIDwithType;

I am expecting the decoded value to be something like this

id@100

so i will explode it by @ and find the user id , here it is 100 .

for testing i have changed the encrypted value by manually adding one or two characters . when i decrypt i got something like

id@100������������������������]u甀�+&�fj�W�ZЪS��d��]3�]"

and after i explode this i will get the id as 100���]u甀�+&�fj�W�ZЪS��d��]3�]"

now i select all raws with same id using .

SELECT * FROM table WHERE id=$id

it will select the recordes with id = 100 even the actual id is 100���]u甀�+&�fj�W�ZЪS��d��]3�]"

so weird . the type of the id column is INT may be that is why it is matched .

but from my point of view it is very bad , because my whole logic got incorrect because of this .

I checked this query both in Laravel and raw MySQL Query , the results are the same .

Any Ideas , Thanks in advance .

UPDATE

I understand the point mentioned by Shadow, but how to handle a scenario like this , from decryption i expect id like 100 , but if i got something like 1ASASAS, if you cast it to int it will be 1 (this is just for example) . now the problem is my database has a user id 1 also , so now you can see how much trouble i am in because i will get an incorrect user , sadly this is related to payment :P . some incorrect users wallet will be topped up . ha ha .how to handle this

  • 写回答

1条回答 默认 最新

  • dongmeng2687 2016-11-10 09:55
    关注

    This is not a bug, this is a feature in MySQL described in Type Conversion in Expression Evaluation section of the MySQL manual:

    When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.

    When MySQL converts a string to a number, it evaluates the characters starting from the left as long as the characters can be interpreted as part of a number and stops, if it encounters a character that cannot be considered as part of a number. In case of 100���]u甀�+&�fj�W�ZЪS��d��], the first cannot be interpreted as a part of the number, so MySQL stops after 100. The characters after 100 seem to be some kind of garbage anyway and you should check your php code why it produces that garbage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?