I don't have much Joomla or PHP experience so I'm probably missing something really simple. I'm trying to load a certain list of user IDs into an array. This code works except it only loads one result.

$sql = 'SELECT user_id FROM #__user_usergroup_map WHERE group_id = ' . $usergroup;
$usersInGroup = array();
$usersInGroup[] = $this->app->database->queryResult($sql);

Does queryresult() only support single results? Is there a different method that I can use that supports multiple results?




3 Answers 3


The "official" Joomla way is to use the JDatabaseQuery class to create the query and give it to the Database layer. Then you use the loadAssocList or loadObjectList function to get a array with all rows.

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->where($db->quoteName('group_id').' = ' . $db->quote($usergroup));
$result = $db->loadObjectList(); // alternative $db->loadAssocList();

The $result is a array with all rows from the Database.


If your query is meant to return just one value per row, then you are wanting to return a "column" of data. This is best packaged as an indexed, one-dimensional array.

loadColumn() is the best tool for the job.

$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->where("group_id = " . $db->quote($usergroup));
$ids = $db->loadColumn();
$db = JFactory::getDbo();
$list = $db->setQuery('select * from #__users')->loadAssocList();
  • 1
    Could you maybe explain why your code will work? Also, the table you have defined is incorrect
    – Lodder
    Commented Apr 10, 2015 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.