Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ordering by ASC id but latest inserts

Featured Replies

Hello, for my message system im trying to find the easiest way to get the latest messages rather than all of them, i initially tried

 

$sqlquery = "SELECT * FROM `messages` WHERE ((uid1='$cid' OR uid2 ='$cid') AND (uid1='$otherid' OR uid2 ='$otherid')) ORDER BY id ASC LIMIT 20";

 

but obviously thats bringing the first 20 messages ever sent, if your slightly confused why its the first 20 not the last, its because my messaging system is showing newest messages from bottom to oldest at the top (like facebook and similar messaging systems)

 

Any help? Thanks, Gary

Edited by web-itec

If the ID field is auto-incrementing, the newest posts will always have the highest values. If you order by DESC instead, you'll get the latest 20. If they're in the wrong order you can assign the returned values to an array and reverse it before you use it.

 

Edited for clarity:

 

ASC vs DESC looks at the whole dataset or table to get the values, and takes the number specified by LIMIT from either the top (DESC) or the bottom (ASC).

Edited by Renaissance-Design

  • Author

If the ID field is auto-incrementing, the newest posts will always have the highest values. If you order by DESC instead, you'll get the latest 20. If they're in the wrong order you can assign the returned values to an array and reverse it before you use it.

 

Edited for clarity:

 

ASC vs DESC looks at the whole dataset or table to get the values, and takes the number specified by LIMIT from either the top (DESC) or the bottom (ASC).

 

good idea :D thanks +1

  • Author

just to let you know, i got this to work pretty easily, the only problem was though, is it took just as long to load as before when loading in all the messages, so im just goin to reverse the whole thing all together and use limit on descending order, thanks anyway :)

Are there indexes on uid1 and uid2? Are there fields in the table you don't need for this specific query?

 

Indexing uid1 and uid2 and specifying only the fields you want should really boost the performance of this particular query.

  • Author

Are there indexes on uid1 and uid2? Are there fields in the table you don't need for this specific query?

 

Indexing uid1 and uid2 and specifying only the fields you want should really boost the performance of this particular query.

 

they arent indexed no, ive just reversed everything now anyway and using limit, but thanks anyway mate, much appreciated :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.