Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PHP's crc32 hash into the MySQL equivalent?

Tags:

sql

php

hash

Apparently MySQL's CRC32() function returns an unsigned BIGINT, while PHP returns hexadecimal value.

In PHP:

hash('crc32','hello world') == 7813f744

In MySQL:

SELECT CRC32('hello world') == 222957957

The PHP CRC32 value is stored in a CHAR(8) column.

I can't figure out how to turn the PHP generated hash into the same value that MySQL produces only using SQL. The obvious doesn't seem to work:

SELECT HEX(CRC32('hello world')) == D4A1185

SELECT CONV('7813f744',16,10) == 2014574404

Any ideas?

like image 874
DanMan Avatar asked Oct 26 '12 11:10

DanMan


1 Answers

If you have 64-bit platform you can safely use crc32 function in PHP and CRC32 in MySQL. Quick test:

  php > echo crc32('foobar') . "\n";
  2666930069

MySQL:

  >select crc32('foobar');
  +-----------------+
  | crc32('foobar') |
  +-----------------+
  |      2666930069 |
  +-----------------+
  1 row in set (0.00 sec)
like image 127
mente Avatar answered Oct 13 '22 22:10

mente