Search code examples
phpmysqlsqlfiddle

MYSQL unhex() in sqlfiddle.com is not equivalent to PHP hex2bin(), both give different results


I'm trying to use PHP instead of MYSQL to add key values in database

select unhex(md5('google.com')) from x;

Output

HVkg9LRLJ6gCvXfE8FNvWg==

however using PHP

echo hex2bin((md5("google.com")));

Output

Y ��K'��w��SoZ

I'm not sure what is going wrong here, any help ?

Edit1 Joachim Isaksson

enter image description here


Solution

  • I'm not sure how you're getting that value, however doing your select at a MySQL prompt gives the same value as in PHP;

    mysql> select unhex(md5('google.com'));
    +--------------------------+
    | unhex(md5('google.com')) |
    +--------------------------+
    | Y �K'��w��SoZ         |
    +--------------------------+
    1 row in set (0.00 sec)
    

    What you're displaying is the base64 encoded value of the same thing;

    mysql> select to_base64(unhex(md5('google.com')));
    +-------------------------------------+
    | to_base64(unhex(md5('google.com'))) |
    +-------------------------------------+
    | HVkg9LRLJ6gCvXfE8FNvWg==            |
    +-------------------------------------+
    1 row in set (0.00 sec)
    

    How your value gets base64 encoded I'm not sure, however it seems to have nothing to do with the MySQL query you're showing. It may be caused by how you're fetching the value in PHP.