Jump to content

Listing previous 10 records


Recommended Posts

I want to list the previous (minus) 10 records.
classic asp example:  <% do while not rs.eof and i< 7 %>
how to do with php pdo?

My code is this:
<?php
$sqlq=$connectx->prepare("SELECT * FROM lovetree ");
$sqlq->execute();
while($rs = $sqlq->fetch()) {

echo $rs['tree'];

}
?>

Thanks...

Link to post
Share on other sites

What do you mean by "previous" 10 records? If you are trying to limit the number of records in your query use LIMIT 10. I'm not sure this is a PHP question as opposed to a MySQL question.

Edited by gw1500se
Link to post
Share on other sites

Then add DESC to the LIMIT clause:

$sqlq=$connectx->prepare("SELECT * FROM lovetree DESC LIMIT 10");

That sorts in descending order which means the rows are upside down so the limit 10 results in the last 10 rows. Note the first row returned was the last row in the table. You may want to resort the resulting array to turn it right side up, if that matters.

Edited by gw1500se
Link to post
Share on other sites

thank you but this is not the solution.
As if there were no first 10 records - next 10 records will be print to the screen.

$sqlq=$connectx->prepare("SELECT * FROM lovetree WHERE id > -10");
- 10 record go back

Link to post
Share on other sites
1 hour ago, DisplayError said:

As if there were no first 10 records - next 10 records will be print to the screen.

How can that situation even be possible?

@DisplayError If you tell us what you are trying to do in a clear, logical manner then we can probably help. Otherwise you're on your own.

Link to post
Share on other sites

To get the records you highlighted

SELECT id
     , tablex
FROM lovetree
LIMIT 9, 11;

i.e. LIMIT offset, num_recs

However, you probably meant LIMIT 10, 10 but I can only guess.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.