DisplayError Posted March 25, 2021 Share Posted March 25, 2021 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... Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/ Share on other sites More sharing options...
gw1500se Posted March 25, 2021 Share Posted March 25, 2021 (edited) 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 March 25, 2021 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585378 Share on other sites More sharing options...
DisplayError Posted March 25, 2021 Author Share Posted March 25, 2021 I want to list 10 records after the last 10 entrie. Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585380 Share on other sites More sharing options...
gw1500se Posted March 25, 2021 Share Posted March 25, 2021 (edited) 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 March 25, 2021 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585381 Share on other sites More sharing options...
DisplayError Posted March 25, 2021 Author Share Posted March 25, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585382 Share on other sites More sharing options...
gw1500se Posted March 25, 2021 Share Posted March 25, 2021 Sorry, I don't understand what you are asking. Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585384 Share on other sites More sharing options...
Barand Posted March 25, 2021 Share Posted March 25, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585385 Share on other sites More sharing options...
dodgeitorelse3 Posted March 25, 2021 Share Posted March 25, 2021 OP is most likely after rows 11 through 20 (after last 10) Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585387 Share on other sites More sharing options...
Barand Posted March 25, 2021 Share Posted March 25, 2021 Maybe, but only if the first 10 don't exist ??? - who can tell? Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585388 Share on other sites More sharing options...
DisplayError Posted March 25, 2021 Author Share Posted March 25, 2021 Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585389 Share on other sites More sharing options...
Barand Posted March 25, 2021 Share Posted March 25, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585390 Share on other sites More sharing options...
DisplayError Posted March 25, 2021 Author Share Posted March 25, 2021 <?PHP Beautiful people, i wish love and health ?> 🖐️ Quote Link to comment https://forums.phpfreaks.com/topic/312381-listing-previous-10-records/#findComment-1585391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.