Sql limit not working

hanees

New Member
I am trying to get 2nd last record using below code in 'Default' option. but it outputs the last record not 2nd last record. what is the issue?

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('name')->from('mytable')->order('id DESC')->setLimit('1,2');
$db->setQuery($query);
return $db->loadColumn();
 
Just to explain that one, when you provide two args to MySQL's limit clause, they are (offset, limit), where 'offset' is the row count you want to start at, which numbers from 0 (so an offset of 0 is the first row in the result set), and 'count' is the number of rows to return. So your initial attempt of (1,2) would have returned two rows (limit of 2), starting at the second (offset of 1). Although because you are using a single row selection method like loadColumn(), you would only see data from the first of the selected rows.

So to get the second, you need (1,1), an offset of 1, and a limit of 1.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top