mysql 自动生成crc32_php – 如何将crc32存储在MySQL中?

我在PHP中创建一个crc32,需要将它存储在MySQL数据库的一个字段中.在阅读了关于32位与64位机器上的结果如何关注之后,我想知道应该如何存储这个数字.这就是我在PHP中处理crc32以在任何bitsize机器上获得相同结果的方式:

$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("%u\n", $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类型(签名长).目前,您只能通过使用pack()和unpack()来实现此目的:

echo current(unpack('l', pack('l', $hash)));

// returns -2103228862 on both 32-bit and 64-bit platforms

标签:php,mysql

来源: https://codeday.me/bug/20190625/1287391.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值