php – 如何将crc32存储在MySQL中?
发布时间:2020-12-13 17:14:12 所属栏目:PHP教程 来源:网络整理
导读:我在 PHP中创建一个crc32,需要将它存储在MySQL数据库的一个字段中.在阅读了关于32位与64位机器上的结果如何关注之后,我想知道应该如何存储这个数字.这就是我在PHP中处理crc32以在任何bitsize机器上获得相同结果的方式: ?php$checksum = crc32("The quick br
我在
PHP中创建一个crc32,需要将它存储在MySQL数据库的一个字段中.在阅读了关于32位与64位机器上的结果如何关注之后,我想知道应该如何存储这个数字.这就是我在PHP中处理crc32以在任何bitsize机器上获得相同结果的方式:
<?php $checksum = crc32("The quick brown fox jumped over the lazy dog."); // On a 32-bit system it prints -2103228862 instead of // 2191738434 which is correct and what prints on a 64-bit system. // See the php.net manual page on crc32 for more information about // 32-bit vs 64-bit. echo "checksum without printf formatting: " . $checksum . "n"; printf("%un",$checksum); $string = sprintf("%u",$checksum); echo $string . "n"; ?> 输出(在64位机器上): checksum without printf formatting: 2191738434 2191738434 2191738434 该数字应如何存储在MySQL上?到目前为止,我已经提出了几个选择: `hash1` CHAR(10) NOT NULL,`hash2` varchar(32) NOT NULL,`hash3` int unsigned NOT NULL, 看起来我应该选择: `hash4` BIGINT UNSIGNED NOT NULL, 解决方法
您可以将值存储在MySQL中作为INT UNSIGNED,它占用4个字节(即32位).
要将值插入数据库,必须在32位计算机上使用带有%u格式的sprintf(): $hash = crc32("The quick brown fox jumped over the lazy dog."); $stmt = $db->prepare('INSERT INTO mytable VALUES (:hash)'); $stmt->execute(array( ':hash' => sprintf('%u',$hash),)); 更新 您还可以确保在32位和64位平台上始终使用int32类型(签名长).目前,您只能通过使用 echo current(unpack('l',pack('l',$hash))); // returns -2103228862 on both 32-bit and 64-bit platforms 这个想法是由mindplay.dk提供的 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |