MD5(RAND()) on MySQL returning only digits

2019-07-02 02:31发布

I'm running this sample code I found while googling:

SELECT MD5(RAND())

But, to my surprise, MD5 is returning plain digits, instead of hexadecimal digits. Using CONV(MD5(RAND()), 10, 16) seems to solve my problem, but the MySQL documentation states that the MD5 function should return a string already in hexadecimal.

Am I doing something wrong?

EDIT2: This problem only appears to exist with phpMyAdmin, not the command-line version of MySQL.

EDIT: My MySQL version:

mysql --version
mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (x86_64) using readline 6.1

An example MD5 value:

6338666264663132376461666163313063656535633666616266396530613335

Also, CONV returns a very VERY short string, like:

B9

This string is often single-digit and the longest I got until now was 5 digits.

2条回答
forever°为你锁心
2楼-- · 2019-07-02 03:04

Nevermind, I figured it out.

Somehow, phpMyAdmin is reading the result as binary. There seems to be an option "Show binary contents as HEX" that's checked by default.

It appears that this "show as hex" option is causing the problems. Unchecking it seems to solve it.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-02 03:13

The MySQL documentation does suggest that the result should be a string or NULL no matter what. At a guess, could this be happening because the argument to MD5() is a number? What happens if you use SELECT MD5(CONCAT(RAND(), ''))?

查看更多
登录 后发表回答