how to randomize retrieval of question from database?

后端 未结 5 1600
南旧
南旧 2021-01-20 15:06

.i have the following code:



        
5条回答
  •  不要未来只要你来
    2021-01-20 15:09

    You can use LIMIT m,n to both limit the number of results you get and offset the results by a given amount.

    Now you could do something like:

     SELECT * FROM questions WHERE QuizID=1 LIMIT $page,5;
    

    Where you calculate the $page based on a $_GET variable. But this won't solve your randomness.

    You could always seed RAND($key) by a given key that you save in your session so you could ORDER BY RAND($key) and use the above limit technique.

    Probably the simplest to implement would be to get the entire result set, shuffle it and cache it. Then use a php to show only a specific chunk of the cache.

    Since this is related to pagination. Let me tell you, LIMIT m,n may not be as fast as it sounds. Learn how to improve it and read more about Efficient Pagination Using MySQL

提交回复
热议问题