Php I';我没有得到正确的随机值。我的数据库中有6个值。我给限制3。但前4项是随机的

Php I';我没有得到正确的随机值。我的数据库中有6个值。我给限制3。但前4项是随机的,php,mysql,sql,web,Php,Mysql,Sql,Web,我需要6个人的3个问题。但每次随机显示快4个问题 /*for random question*/ public function qustionShow($question){ $query = $this->conn->query("select * from question where cat_id='$question'"); $c = mysqli_num_rows($query); $rand = rand(3, $c)-3;

我需要6个人的3个问题。但每次随机显示快4个问题

    /*for random question*/
public function qustionShow($question){

    $query = $this->conn->query("select * from question where cat_id='$question'");
    $c = mysqli_num_rows($query);
    $rand = rand(3, $c)-3;


    $show = $this->conn->query("select * from question where cat_id ='$question' and id >'$rand' LIMIT 3");
    while ($row=$show->fetch_array(MYSQLI_ASSOC)){
        $this->qus[]=$row;
    }
    return $this->qus;  
}

您可以使用ORDER BY RAND()减少和优化代码

尝试以下方法:

public function qustionShow($question, $limit=3){
    $show = $this->conn->query("select * from question where cat_id ='$question' ORDER BY RAND() LIMIT $limit");
    while ($row=$show->fetch_array(MYSQLI_ASSOC)){
        $this->qus[]=$row;
    }
    return $this->qus;  
}

如果限制
..='$QUOTE'和id>'$rand'限制4
3
吗?哦,对不起,我在代码中使用了3。^^^^^值得怀疑,它是如何出现的?我从4个问题中得到了3个,但我的数据库中有6个问题。