gpt4 book ai didi

PHP 缺少无符号整数和 MySQL 的 CRC32 函数

转载 作者:可可西里 更新时间:2023-11-01 07:37:53 26 4
gpt4 key购买 nike

我告诉 Sphinx 以 CRC32 形式索引一些字符串作为属性,如下所示:

sql_query = SELECT [...], CRC32(LOWER(color)) AS color, [...], FROM table

sql_attr_uint = color

我正在尝试在 PHP 中进行一些分面搜索,用户可以在其中单击具有上述 colors 之一的链接,Sphinx 将获得另一个搜索请求,其中包含缩小的结果,一些东西喜欢:

Previous page:
<h3>Narrow down results:</h3>
<p><a href="search.php?query=something&color=1678454">Red (11)</a></p>
<p><a href="search.php?query=something&color=4133605867">Yellow (5)</a></p>

<?php
if (isset($_GET[$name]))
{
$sphinx->SetFilter('color', intval($_GET['color'])); // <-- Uh-oh
}

[...]

$sphinx->Query($query, 'table');
?>

这里出错是因为 MySQL 的 CRC32() 返回一个无符号的 32 位整数,而 PHP 非常有帮助地不支持它并且上限为 2^31-1。上面黄色的链接暴露了我的问题。

StackOverflow,什么是可以接受的解决方法?我想在 SQL 查询端做一些数学运算是可能的,但我担心让令人担忧的碰撞机会变得更糟。

提前致谢。

最佳答案

引用自 PHP 的 crc32() manual page :

Because PHP's integer type is signed, and many crc32 checksums will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32 checksum.

换句话说,PHP 无法处理大整数,但在这种情况下,您可以模拟 无符号整数为 double 。转换为字符串时可以获得真实值。

这应该足够了,因为您不需要进行数学运算,例如加法或乘法。


更新:我想我刚刚忽略了您问题的重要部分。而不是这样:

intval($_GET['color'])

...您可以使用正则表达式进行验证或过滤:

preg_match('/^\d+$/', $_GET['color'])
preg_replace('[^\d]', '', $_GET['color'])

... 并将 CRC 值作为字符串处理。

关于PHP 缺少无符号整数和 MySQL 的 CRC32 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com