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.

3
  • Please, try print_r($results). Commented Sep 30, 2014 at 22:56
  • use print_r($results) to find the structure. You can also use foreach() -> foreach($results as $result){ print_r($result);} Commented Sep 30, 2014 at 22:56
  • Check the answer of this question. Btw, assuming id is an integer, please treat it as such: "..where id=15.." Commented Sep 30, 2014 at 22:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.