title: How to: MySQL order by user_id (RAND) with pagination
body: <p>Looking for a solution to keep a random order of a user table in the database when clicking the next page button.</p>
  
  <p>Actually I have a database with 1000 users and I want to display 10 users each page (in a memberlist), my query looks like this:</p>
  
  <pre><code>$sql = "SELECT * FROM users ORDER BY user_id LIMIT 1,10";
  </code></pre>
  
  <p>Now I would like to ORDER BY RAND() and it works, except of course when clicking the next page, then it is shuffled again and it happens sometimes that the same users will be there again.</p>
  
  <p>So my question is about a solution to keep the random order I had on the first page, also on the next pages.</p>
  
  <p>I thought about to set a $_SESSION variable when someone visits the memberlist for the first time with <strong>shuffled numbers from 1 to 1000</strong> in it and then order the members by position in the $_SESSION variable <strong>where a number is equal to a user_id</strong>.</p>
  
  <p>Don't know how this might be possible, but I actually imagine a solution like:</p>
  
  <pre><code>$numbers = range(1, 1000);
  
  $shuffled_numbers = shuffle($numbers);
  
  $sort = $_SESSION['random_user_sort'] = $shuffled_numbers;
  </code></pre>
  
  <p>So I will have a mysql query when clicking page two (next page) like this:</p>
  
  <pre><code>$sql = "SELECT * FROM users ORDER BY $sort LIMIT 11,20";
  </code></pre>
  
  <p>Any solution to let it work this way or even better ideas?</p>