title: Pagination of MySQL results in PHP
body: <p>I never really considered this(pagination) as an issue until lately. When I sat down and zeroed in on it, I found myself facing plenty of problems.</p>
  
  <p>What I am into is a basic contacts management system wherein a user can add/update/delete/search contacts. The search part is where I need the pagination to be implemented effectively.</p>
  
  <p>What I have in mind (with +ve and -ve points)</p>
  
  <ul>
  <li>I can specify <code>pageNo</code> and <code>offset</code> while <code>POST</code>ing to my <code>search.php</code> page. This page will fire a simple MySQL query to retrieve the results. Since the number of rows can pretty much run in thousands, I need to paginate it. Quite simple, but I need to fire the <em>same</em> query again and again for every different page. Meaning, when a user goes from page1 to page2, the same MySQL query will be fired(of course with a different <code>offset</code>), which is something I feel is redundant, and am trying to avoid.</li>
  <li>Then I thought of capturing the <strong>entire</strong> set of result, and storing it into <code>$_SESSION</code>, but in this case, what if the results are just <em>huge</em>? Will it affect performance in any way?</li>
  <li>On similar lines like the second point, I thought of writing out the results on to a file, which is plain crap! (I just put it here, as a point. I know this is REAL bad way of doing things.)</li>
  </ul>
  
  <p>My Questions:</p>
  
  <p><strong>A</strong>. Which of the above methods do I implement? Which one is better? Are there any other methods? I have googled it, but I find that most of the examples follow point1 above.</p>
  
  <p><strong>B</strong>. My questions for point1: How can we rely on the order of the mysql results? Suppose, the user navigates to page2 after some time, how can we be sure, that during the second time, the records of the first page arent repeated? (Because, we are doing a fresh query)..</p>
  
  <p><strong>C</strong>. What exactly is a MySQL resource? I understand that a <code>mysql_query(..)</code> returns a <code>resource</code>. Is it global in the sense that, it maintains the state between different calls to PHP script? (I can maintain the resource in a <code>$_SESSION</code>).</p>
  
  <p>Thanks a million! :-)</p>
  
  <p>PS: I know this is a pretty long question. I just tried to put across, in a concise way, whats going around in my head.</p>