MySQLi LIMIT 1 OFFSET 1 not working
I may be wrong, but it seems that you can't use the MySQL parameter OFFSET to find the penultimate record in a list.
For example, to find a User's previous login, the syntax would be something like:
SELECT id_log, datein_log FROM logger_log WHERE idusr_log= ? ORDER BY id_log DESC LIMIT 1 OFFSET 1
Unfortunately, this throws an error. I can't find the correct syntax on the WebAssist Community Forums, so I came up with a work around.
First find the Last Record, then use the ID of this record to find the Penultimate Record, whose ID will be less than the Last Record, as follows:
<?php
$rsLastLogin = new WA_MySQLi_RS("rsLastLogin",$yourdbconnection,1);
$rsLastLogin->setQuery("SELECT id_log, datein_log FROM logger_log WHERE idusr_log= ? ORDER BY id_log DESC");
$rsLastLogin->bindParam("i", "".$_SESSION['wa_user_id'] ."", "-1"); //colname
$rsLastLogin->execute();
?>
<?php $lastid = $rsLastLogin->getColumnVal("id_log"); ?>
<?php
$rsPenultimateLogin = new WA_MySQLi_RS("rsPenultimateLogin",$yourdbconnection,1);
$rsPenultimateLogin->setQuery("SELECT id_log, datein_log FROM logger_log WHERE idusr_log= ? AND id_log < ? ORDER BY id_log DESC");
$rsPenultimateLogin->bindParam("i", "".$_SESSION['wa_user_id'] ."", "-1"); //colname
$rsPenultimateLogin->bindParam("i", "".$lastid ."", "-1"); //colname2
$rsPenultimateLogin->execute();
?>
Then in your page, display in your preferred format:
<p>Last Login: <?php echo(date('d F Y H:i', strtotime($rsPenultimateLogin->getColumnVal("datein_log")))); ?></p>
A bit longwinded maybe, but I hope this helps anyone with the same issue.