0

I got the following code:

$comments = $db->QueryFetchArray("SELECT * FROM `ycommentbox` WHERE `url`='".$id."'");
foreach ($comments as $comment) {
    echo $comment;
}

which echoes:

"test121677129055"

"ycomemntbox" structure:

three rows

Why isn't it echoing the other 2 rows but just the first one?

It should echo this:

"test121677129055"
"test221677129056"
"test321677129057"

2
  • 1
    Have you tried var_export($comments); to see the data structured being returned by QueryFetchArray? What does the result look like? Commented Dec 29, 2014 at 4:05
  • 1
    QueryFetchArray() is not a standard function, so we do not know what it returns. It may fetch one row, or maybe many. Commented Dec 29, 2014 at 4:06

4 Answers 4

1

Because QueryFetchArray only returns a single row; it's only an array because it includes all the column values from that row.

To get all the rows, you need to either do QueryFetchAll to get them in one big array, or do an initial Query followed by FetchArray in a loop that will run once per row.

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

Comments

1

Try this

And your id wants to match with this query :

 $comments = $db->QueryFetchAll("SELECT * FROM `ycommentbox` WHERE `url`='".$id."'");
    foreach ($comments as $comment) {
        echo $comment;
    }

OR you can change this query like this

$comments = $db->QueryFetchAll("SELECT * FROM `ycommentbox`");
        foreach ($comments as $comment) {
            echo $comment;
        }

1 Comment

You're missing a " in the second example
1

You can use print_r for multiple values
and use echo for single value ok
OR:
If you use only echo then use echo & tr td html tags between "".
Write starting tags in "" and .$variable. and again end tags between `""

1 Comment

Can you give an example? It's a little difficult to visualize what your are trying to describe. Thanks.
0

For loop looks ok, so I can suggest first to make a {echo count($comments);} to check actually how many rows returned by the query. It will validate the query first.

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.