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

我告诉Sphinx将CRC32表单中的一些字符串作为属性索引,如下所示:

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

sql_attr_uint = color

我正试图在PHP中进行一些分面搜索,用户可以点击上述颜色之一的链接,而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.上面的黄色链接暴露了我的问题.

*,这可能是一个可接受的解决方法吗?我想在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无法处理大整数,但在这种精确的情况下,您可以模拟无符号整数以获得双精度.在转换为字符串时,您可以获得真正的价值.

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

更新:我想我只是忽略了你问题的重要部分.而不是这个:

intval($_GET['color'])

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

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

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

上一篇:C++之------类和对象


下一篇:Php与Sphinx的联系遭到拒绝