user863739
user863739

Reputation: 299

PHP MYSQL return from row

I want to know if it is possible to make a mysql_query start art a certain row in the database.

I want to to start at row2 instead of row1

Upvotes: 0

Views: 257

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270599

Use the LIMIT clause, with a very large number (larger than your rowcount) for the max records returned. Normally, LIMIT is used to select a block of records, optionally beginning with an offset. In your case, you want all records after the offset so you have to fake it with a high maximum number.

UPDATE I just read the docs and LIMIT starts its offset at 0, so it should in fact be LIMIT 1, 99999

SELECT * FROM table ORDER BY id ASC LIMIT 1, 99999999999

On the other hand, if your first record has an identity column less than the second row, you can just do:

SELECT * FROM table WHERE id < 1;

Upvotes: 4

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146350

In relational databases like MySQL rows do not have any default order. Seriously. Even if several SELECT * FROM table consecutive queries appear to sort results by insertion order.

You can, however, use the ORDER BY clause to order the rows of specific queries.

Additionally, the LIMIT clause allows further filtering.

Upvotes: 2

Nemanja
Nemanja

Reputation: 1535

You can use offset - to skip a certain number of rows - i. e.

SELECT * FROM table LIMIT 10, 10

This will skip first 10 rows and retrieve the next 10.

Upvotes: 0

John Green
John Green

Reputation: 13435

Depends on what you're trying to do. You can't start at a certain row int he DB, but you can start at a certain row in the result set:

 SELECT * FROM tbl_name LIMIT 2, 10000;

Of if you explicitly need to skip a row for some reason:

$skip = true;
while ($row = mysqli_fetch_assoc($res))
{
    if ($skip)
    {
        $skip = false;
        continue;
    }

Or a variety of other solutions depending on your exact problem.

Upvotes: 0

Related Questions