title: PHP- get previous rows value
body: <p>I have a database that contains the following:</p>
  <pre><code>ID | value
  1  | Hello
  14 | Potato
  34 | Henry
  </code></pre>
  <p>The information is echoed using the following foreach:</p>
  <pre><code>foreach($db-&gt;query(&quot;SELECT * FROM table&quot;) as $X){
   echo $X['ID']; 
   echo $X['value']; }
  </code></pre>
  <p>Is there a way to get the ID from the previous row in the table as a variable?
  So as the result looks something along the lines of this:</p>
  <pre><code>echo &quot;Current ID: &quot;.$X['ID'];
  echo &quot;Current value: &quot;.$X['value'];
  echo &quot;Previous value: &quot;.$PreviousValue;
  </code></pre>
  <p>gives</p>
  <blockquote>
  <p>Current ID: 14</p>
  <p>Current value: Potato</p>
  <p>Previous value: Hello</p>
  </blockquote>