954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Select Statement

Any one know how I can write a php statement to call the last 4 entires from tbl_accounts by userID?

Example:

I have 4 users, userID's 1,2,3and 4. All of these users are showing on the home page. When user #5 signs up, I want user 1 to fall off home page and user 5 to take user 1 spot.

Morty222
Light Poster
29 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
Any one know how I can write a php statement to call the last 4 entires from tbl_accounts by userID?


You need to do two things here.

First, order the results in descending order, so that the latest entry is first in the list. For this, you can use "ORDER BY userID DESC".

Then, you need to grab only the first four entries (the latest four additions). For this, use "LIMIT 4".

Altogether, it should look like this...

$query = "SELECT * FROM tbl_accounts ORDER BY userID DESC LIMIT 4";
Walkere
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 29
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You