My site has a sizable table generated by a mysqli_query; the query includes a column 'messagetime':
$result = mysqli_query($cxn,
"
SELECT ......., messagetime
FROM messages
ORDER by messagetime
DESC
"
);
I then generate the table (in php) with the help of a while loop:
echo "<table id='myTbl' class='newest' border='1'>";
echo "<th>Content</th> //etc.
echo "<tbody>";
while ($row = mysqli_fetch_assoc($result)) {
// Fill the table body here
}
echo "</tbody></table>";
Nothing earth-shaking. However, the site uses polling to refresh the table, and I need to store the messagetime
of the first result of the query in the table, like this:
echo "<table id='myTbl' class='newest".$newestTime."' border='1'>";
Right now, to capture the first (newest) messagetime, I am tweaking my While loop to do this:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($i == 0) {
echo "<table id='myTbl' class='newest".$row['messagetime']."' border='1'>";
echo "<th>Content</th> //etc.
echo "<tbody>";
}
// Fill the table here
$i++;
}
echo "</tbody></table>";
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 here), to simply pluck the first result from the query somehow, something like this?
$result = mysqli_query($cxn,
"
SELECT ......., messagetime, **first_messagetime_result AS newest_time**
FROM messages
ORDER by messagetime
DESC
"
);
Thanks for any insight!