SQL call to find highest ID (General)

by Alise, Latvia, Tuesday, August 23, 2011, 17:48 (4651 days ago)

Hey,

I want to know the highest ID of all the posts in my forum. I use the code below, and it works. But I wonder if it is an efficient way of doing it or if there's a better way. Thanks :-)


$result = mysql_query("SELECT id FROM <Change_to your_DB>.mlf2_entries ORDER BY id DESC LIMIT 0,1");
$highestID = mysql_result($result, 0);

Avatar

SQL call to find highest ID

by Auge ⌂, Wednesday, August 24, 2011, 10:49 (4650 days ago) @ Alise

Hello

I want to know the highest ID of all the posts in my forum. I use the code below, and it works. But I wonder if it is an efficient way of doing it or if there's a better way. Thanks :-)


$result = mysql_query("SELECT id FROM <Change_to your_DB>.mlf2_entries ORDER BY id DESC LIMIT 0,1");
$highestID = mysql_result($result, 0);

Thus query is generally correct. If you use it in the context of the forum script, you use the existing connection to the forums database. So there's no need to declare the database name. The following version of the query should do the same as yours (only cosmetical changes).

SELECT id FROM mlf2_entries ORDER BY id DESC LIMIT 1

Tschö, Auge

--
Trenne niemals Müll, denn er hat nur eine Silbe!

Avatar

SQL call to find highest ID

by Micha ⌂, Wednesday, August 24, 2011, 10:53 (4650 days ago) @ Auge

Hi,

Why not using MAX?

SELECT MAX(id) AS foo...

regards
Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

RSS Feed of thread