How to use LIMIT in getQuery

by GDP   Last Updated June 22, 2018 17:10 PM

Given the desired SQL:

SELECT * FROM #__tablename LIMIT 5

How do I use $query to accomplish it?

$db = JFactory::getDBO();       
$query = $db->getQuery(true);
$query->select($db->nameQuote('*'));
$query->from($db->nameQuote('#__tablename'));      
$db->setQuery($query);      
$rows = $db->loadObjectList();
Tags : jdatabase


Answers 1


Both J2.5 and 3.x will vary.

Joomla 2.5:

$db->setQuery($query, 0, 5); 

where 0 is the start/offset and 5 is the limit

Joomla 3.x

$query->setLimit(5);

This also accepts a 2nd argument which is the offset, but is not required

Lodder
Lodder
December 11, 2014 15:38 PM

Related Questions



JDatabase Update +1

Updated June 08, 2017 11:10 AM

JDatabase SQL AND query

Updated May 14, 2015 21:04 PM

Join two similar table SQL

Updated December 15, 2017 02:10 AM

Very slow query using JDatabase vs Manual

Updated June 18, 2015 07:04 AM