从MySQL Rand()更改为PHP random_int

时间:2017-01-27 07:05:07

标签: php mysql random

我们正在创建一个随机数游戏。我们目前在下面的语句中使用MySQL Rand()函数:

private function doPreEventStart($user) {
$row = db_fetch_item("SELECT resultid FROM ResultPackage 
    where ResultPackage.slotid like '%{$this->curSlotId}'
    and ResultPackage.PackageID like '%{$user->packageid}%'
    ORDER BY RAND() LIMIT 1");
$this->curResultId = $row['resultid'];
}

我们需要将其更改为PHP函数Random_Int。请有人建议通过改变上述代码来做到这一点。从SQL语句中的WHERE条件可以看出,随机数仍然需要在一定范围内。

1 个答案:

答案 0 :(得分:0)

我正在测试此代码:

private function doPreEventStart($user) {
    $row = db_fetch_item("SELECT resultid as MaxResult FROM ResultPackage 
        where ResultPackage.slotid like '%{$this->curSlotId}'
        and ResultPackage.PackageID like '%{$user->packageid}%'
        ORDER BY resultid desc LIMIT 1");
    $this->MaxResult = $row['MaxResult'];


    $row = db_fetch_item("SELECT resultid as MinResult FROM ResultPackage 
        where ResultPackage.slotid like '%{$this->curSlotId}'
        and ResultPackage.PackageID like '%{$user->packageid}%'
        ORDER BY resultid asc LIMIT 1");
    $this->MinResult = $row['MinResult'];

    $this->curResultId = int rand ( int $MinResult , int $MaxResult )
    }