/    Sign up×
Community /Pin to ProfileBookmark

MySQL Select Most Recent Rows

I’m looking for a MySQL command (with php) that selects the most recent row made, or selects all of the row(s) with a certain auto-increment cell being above the number specified.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@clairec666May 28.2004 — Well if you are using auto-increment, the most recent row will be the one with the highest number in the auto-increment column. for example:

$sql = "SELECT * FROM table";

$result = mysql_query($sql, $conn);

$rows = mysql_num_rows($result);

$sql2 = "SELECT *
FROM table where id='$rows'";

A few problems you might come across:

1) If any of the rows have previously been deleted. In that case, $rows won't give you the highest value.....

Instead, you should try $sql = "SELECT * FROM table order by id";

Load it into an array, and take the last number in the array. Then use this in the second sql statement.

2) My typing errors! I'm writing this off the top of my head, so there might be a few mistakes..... sorry!
×

Success!

Help @burozoption spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...