0

I have a php page that calls for data from an sql database and displays it on a table.

while($row = mysqli_fetch_array($result)) {
    echo '<tr>';
    echo "<td style='border:1px solid black;'>{$row['first_name']}</td>";
    echo "<td style='border:1px solid black;'>{$row['last_name']}</td>";
    echo "<td style='border:1px solid black;'><a href=\"javascript:win1({$row['formID']})\">Activities<a/></td>";
    echo "<td style='border:1px solid black;'><a href=\"javascript:win2({$row['formID']})\">Awards<a/></td>";
    echo "<td style='border:1px solid black;'>{$row['total']}</td>"; 
    echo "<td style='border:1px solid black;'>{$row['date']}</td>";
    echo "<td style='border:1px solid black;'>{$row['IP']}</td>";
    echo '</tr>';
}
echo '</table>';

is my current code, but I want to know if there is a way to fetch a certain number row like fetching the 32nd row in the result instead of fetching from 0 to the end?

1
  • When fetching the 32nd row, do you need the others before? Otherwise you could use LIMIT 32,1 in you SQL Statement Commented Sep 15, 2012 at 21:49

3 Answers 3

1

Use LIMIT in SQL Query Like SELECT * FROM table LIMIT x, n this will start from the x+1th row and will select n rows...

http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

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

Comments

0

You can use data_seek in mysqli to do just that

$query = "SELECT * FROM Something";
if ($result = $mysqli->query( $query)) {
    // seek to row number 32
    $result->data_seek(31);
    $row = $result->fetch_row();
    //use the $row data
    $result->close();
}

2 Comments

if I do data seek then loop through with fetch array, will it start from row 31 to the end of the result?
according to php.net/manual/en/mysqli-result.data-seek.php this function (or method) will move the result pointer, in other words, if you fetch after it, it will use seek row as a starting point, if you want to start from somewhere else just reuse it with another row
0

In your SELECT statement, add a LIMIT clause like so: SELECT * FROM my_table WHERE ... LIMIT 31, 1. This will select only the 32nd row from the table. You can replace 31, 1 with any range you want.

This works best if you add an ORDER BY clause so the rows always come out in the same order.

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.