1

How can I get the next row from mysql_fetch_array? I want to get the first and the second row without while.

3 Answers 3

6

Call mysql_fetch_array() a second time.

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

Comments

1

It would be better to do that directly in your SQL query

You can use LIMIT 0, 2;

In that you still use the while but for the exact number of row in your query. Moreover, no need to query your entire table if you only need the first two results.

Comments

1

When you have a while loop, all you essentially do is repeatedly call mysql_fetch_array() as many times as there are rows.

If you want to only show 2 rows, you can simply call mysql_fetch_array() twice, and each time it is called it will grab the next row from the result set.

If you know you're only going to use two results, you should also use a LIMIT 0,2 so you don't load heaps of rows when you are only ever going to use 2, it's a waste of resources and will slow you down over time.

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.