how to get max and min db id?
Moderator: General Moderators
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
how to get max and min db id?
hi there....
is there any way to get the max and min id (primary key) of table using php ??? and 2ndly if there are 50 records in ma table how i can fetch record number 23, mind u it is not id number 23 its record number 23.
Thanx.
Mannan.
is there any way to get the max and min id (primary key) of table using php ??? and 2ndly if there are 50 records in ma table how i can fetch record number 23, mind u it is not id number 23 its record number 23.
Thanx.
Mannan.
One way is to Sort.
I guess you can get the 23rd record by just using LIMIT 22,1 ?
I thought MySQL doesnt store it linearly each time you insert a record.how i can fetch record number 23, mind u it is not id number 23 its record number 23.
I guess you can get the 23rd record by just using LIMIT 22,1 ?
Re: how to get max and min db id?
As far as I understand DB....you should always have something (PK - either auto-increment numeric, several columns or whatever) that will uniqly identify each row in the DB - this is part of the design of course....itsmani1 wrote:hi there....
is there any way to get the max and min id (primary key) of table using php ??? and 2ndly if there are 50 records in ma table how i can fetch record number 23, mind u it is not id number 23 its record number 23.
Thanx.
Mannan.
Otherwise based on what you are looking for row 23?
Yup. The count() function does that.
Tip: take a look at the manual's Array Functions section for more array info.
Tip: take a look at the manual's Array Functions section for more array info.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Code: Select all
//for minimum
select min(fieldname) from tablename
select max(fieldname) from tablenameget the first row of the result.
I dont think you can directly go to a record if you have the whole result of all records
you have to loop and use if condition to find a nth record
Code: Select all
$result = mysql_query("select * from table_name");
$count = mysql_num_rows($query);
for ($i = 0; $i < count; $i++){
if($i == 23)
echo mysql_result($result, $i, "fieldname"); //just one way of doing it
}