0

I have a table called "images" and it has two columns called "id" and "thumb". I want to query all "thumb" values from id "15". Let's say id "15" contains three rows of "thumb" information. This is what I have

        $db = JFactory::getDbo();
        $query = "SELECT * FROM images WHERE id = '15' ORDER BY sort_order DESC";
        $db->setQuery($query);
        $results = $db->loadObjectList();

So now I have $results but when I try to echo them in PHP it doesn't show anything. It says $results is an "array" but I want to display the three values. I would think "echo $results[1];" would she me some data but it didn't show anything. Is there something I am missing here? I know there's something in $results but I am unsure how to display the data.

user1636946
  • 137
  • 1
  • 3
  • 14
  • Please, try print_r($results). – Alejandro Arbiza Sep 30 '14 at 22:56
  • use `print_r($results)` to find the structure. You can also use `foreach()` -> `foreach($results as $result){ print_r($result);}` – Sean Sep 30 '14 at 22:56
  • Check the answer of [this question](http://stackoverflow.com/questions/18187207/db-loadobjectlist-and-mysql-fetch-array-error). Btw, assuming id is an integer, please treat it as such: "..where id=15.." – dimi Sep 30 '14 at 22:58

1 Answers1

0

When a result return by mysql is greater than one, it is return as an array or an object. So when you want to access each row you use while loop to iterate through the array something like this:

    $db = JFactory::getDbo();
    $query = "SELECT * FROM images WHERE id = '15' ORDER BY sort_order DESC";
    $db->setQuery($query);
    while($results = $db->loadObjectList()){
        // echo whatever ; 
    }

Foreach can also be use. It is up to you the programmer to decide which one fits your need.