MoonFox
MoonFox

Reputation: 31

Get next row in mysql_fetch_array

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

Upvotes: 1

Views: 4064

Answers (3)

duellsy
duellsy

Reputation: 8577

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.

Upvotes: 1

Oswald
Oswald

Reputation: 31665

Call mysql_fetch_array() a second time.

Upvotes: 6

TheEwook
TheEwook

Reputation: 11117

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.

Upvotes: 1

Related Questions