I want to show up to 3 images that are assigned to the logged in user in a database table. I have written the following MySQL query:

    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->where('model_id = '.$userid);
    $rows = $db->loadObject();

    $gallery_img = $rows->file;

If I echo $gallery_img this outputs only one of the images, if I look in the database table (for the logged in user) there are 3 images assigned to that user:

id      file                    model_id

389     10_burgers_food.jpeg    941
390     14_bridge.jpeg          941
391     test_imag.jpeg          941

How can I show the 3 images if there's only 1 variable $gallery_img?

1 Answer 1


Problem being is you're using loadObject() which will only ever return 1 result. If you want multiple, you'll need to use loadObjectList(), like so:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
      ->where($db->qn('model_id') . ' = ' . $userid)

$rows = $db->loadObjectList();

foreach ($rows as $row)
    echo '<p>' . $row->file . '</p>';

Just incase there will ever be more than 3 images in the database, you can also limit the results in the query to 3, like so:

      ->where($db->qn('model_id') . ' = ' . $userid)
      ->setLimit('3'); // <-- Here

Hope this helps


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.