Page 1 of 1
how to get max and min db id?
Posted: Sat Aug 20, 2005 3:27 am
by itsmani1
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.
Posted: Sat Aug 20, 2005 3:49 am
by anjanesh
One way is to
Sort.
how i can fetch record number 23, mind u it is not id number 23 its record number 23.
I thought MySQL doesnt store it linearly each time you insert a record.
I guess you can get the 23rd record by just using LIMIT 22,1 ?
Re: how to get max and min db id?
Posted: Sat Aug 20, 2005 4:39 am
by jmut
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.
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....
Otherwise based on what you are looking for row 23?
Posted: Sat Aug 20, 2005 4:56 am
by itsmani1
i have an array $b[] of 10 elements and i wanted to fetch $b[3] is that possible.
Thanx.
Posted: Sat Aug 20, 2005 5:08 am
by m3mn0n
Yes. Just echo it. Tell us how it goes.
Tip: use
print_r() for debugging. Helps when working with large arrays.
Posted: Sat Aug 20, 2005 5:11 am
by itsmani1
yeah its working one last thing is there any way to find array length ?
Thanx.
Posted: Sat Aug 20, 2005 5:15 am
by m3mn0n
Yup. The
count() function does that.
Tip: take a look at the manual's
Array Functions section for more array info.

Posted: Sat Aug 20, 2005 9:43 am
by josh
You could have answered all these questions yourself by looking at the manuals.
Posted: Sat Aug 20, 2005 10:49 am
by raghavan20
Code: Select all
//for minimum
select min(fieldname) from tablename
select max(fieldname) from tablename
run mysql_query($query) for the ones above
get 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
}