php只从mysql中检索一次随机行

I'm trying do display random question for a quiz but I don't want repeat the same question in the quiz.

What I'm currently trying: function in my model to get a random question

public function getRandom()
{
$idarray = $this->session->userdata('list');
//get a random question
$sql = "SELECT * FROM questions_table ORDER BY RAND() LIMIT 1";
$query = $this->db->query($sql);
$row = $query->row();

//make sure same question is not displayed twice ?
while(in_array($row->id, $idarray))
{
    $sql = "SELECT * FROM questions_table ORDER BY RAND() LIMIT 1";
    $query = $this->db->query($sql);
    $row = $query->row();
}

$idarray[] = $row->id;
$this->session->set_userdata('list',$idarray);




//get answers for that question and shuffle them
$query2 = $this->db->get_where('answers_table', array('qid' => $row->id));
$answers = $query2->result();
$shuff = (array)$answers;
shuffle($shuff);

return array('question' => $row, 'answers' => $shuff);

}

In my controller I have a function that is called when the quiz starts

$idarray = array();
$this->session_set_userdata('list',$idarray);

But when i try to run the quiz it doesn't respond.

Tried this:

public function getRandom($ids) { //ids is a session array passed in from the controller

$random = rand(0, count($ids) -1);
echo count($ids);

unset($ids[$random]);
array_values($ids);
$this->session->set_userdata('idlist',$ids);
echo $random;

}

Problem is sometimes the array doesn't decrease and same question is displayed i.e first count is 4 then 3 then 2 then 2 instead of 1.

You could try something like this (untested);

function selectAQuestionId($ids) {
    $id = rand(0, count($ids));

    // Now we need to remove the question id from the ids array
    unset($ids[$id]);

    // And re-index the array, you need to do this
    array_values($ids);

    // Store the questionId and remaining question Ids, in an array to return
    $response['questionId'] = $id;
    $response['ids'] = $ids;

    // Return the question and remaining ids
    return $response;
}

// Grab a list of question id's from your database
// i.e. "SELECT id FROM questions"
$questionIds = array(1,2,3,4,5,6);

// Grab a random question id and store the remaining questions ids
$response = selectAQuestionId(ids);

$questionId = $response['questionId'];
$ids = $response['ids']; // Ids now only contains the remaining question ids

// Use the $questionId, go get your question from the database

Next time you want to get another question, simple passed the returned ids array over to the selectAQuestionId function again.