2个hash table 函数



//最多产生10个表(0、1、2....9)

<pre name="code" class="php">    /**
     * @param string $table_name 表名
     * @param int $user_id 用户id
     * @param int $total 分表总数
     * @link http://www.phpddt.com
     */
    function hash_table($table_name, $user_id, $total)
    {
        return $table_name . '_' . (($user_id % $total) + 1);
    }
     
    echo hash_table("artice", 1234, 5); //artice_5
    echo hash_table("artice", 3243, 5); //artice_4


 


//最多可产生99个表


    function get_hash_table($table, $userid) 
    {
        $str = crc32($userid);
        
        if ($str < 0) {
            $hash = "0" . substr(abs($str), 0, 1);
        } else {
            $hash = substr($str, 0, 2);
        }
     
        return $table . "_" . $hash;
    }
     
    echo get_hash_table('message', 'user18991'); //结果为message_10
    echo get_hash_table('message', 'user34523'); //结果为message_13


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值