title: Is there any way to pluck first result from a query result pre-While loop?
body: <p>My site has a sizable table generated by a mysqli_query; the query includes a column 'messagetime':</p>
  
  <pre><code>$result = mysqli_query($cxn, 
  "
    SELECT ......., messagetime
    FROM messages 
    ORDER by messagetime 
    DESC
  "
  );
  </code></pre>
  
  <p>I then generate the table (in php) with the help of a while loop:</p>
  
  <pre><code>echo "&lt;table id='myTbl' class='newest' border='1'&gt;";
  echo "&lt;th&gt;Content&lt;/th&gt; //etc.
  echo "&lt;tbody&gt;";
  
  while ($row = mysqli_fetch_assoc($result)) {
  
      // Fill the table body here
  
  }
  
  echo "&lt;/tbody&gt;&lt;/table&gt;";
  </code></pre>
  
  <p>Nothing earth-shaking. However, the site uses polling to refresh the table, and I need to store the <code>messagetime</code> of the first result of the query in the table, like this:</p>
  
  <pre><code>echo "&lt;table id='myTbl' class='newest".$newestTime."' border='1'&gt;";
  </code></pre>
  
  <p>Right now, to capture the first (newest) messagetime, I am tweaking my While loop to do this:</p>
  
  <pre><code>$i = 0;
  while ($row = mysqli_fetch_assoc($result)) {
  
       if ($i == 0) {
           echo "&lt;table id='myTbl' class='newest".$row['messagetime']."' border='1'&gt;";
           echo "&lt;th&gt;Content&lt;/th&gt; //etc.
           echo "&lt;tbody&gt;";
       }
  
      // Fill the table here
  
       $i++;
  }
  
  echo "&lt;/tbody&gt;&lt;/table&gt;";
  </code></pre>
  
  <p>However, this seems kind of hacky to me. Is there any way, without a subquery or union (since my query is actually the product of 2 joins, and is already expensive; this approach was described <a href="https://stackoverflow.com/questions/4899042/how-to-select-first-and-last-data-row-from-a-mysql-result">here</a>), to simply pluck the first result from the query somehow, something like this?</p>
  
  <pre><code>$result = mysqli_query($cxn, 
  "
    SELECT ......., messagetime, **first_messagetime_result AS newest_time**
    FROM messages 
    ORDER by messagetime 
    DESC
  "
  );
  </code></pre>
  
  <p>Thanks for any insight!</p>