how to get max and min db id?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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?

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 ?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: how to get max and min db id?

Post 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?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

i have an array $b[] of 10 elements and i wanted to fetch $b[3] is that possible.

Thanx.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yes. Just echo it. Tell us how it goes.

Tip: use print_r() for debugging. Helps when working with large arrays.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

yeah its working one last thing is there any way to find array length ?

Thanx.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yup. The count() function does that.

Tip: take a look at the manual's Array Functions section for more array info. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

You could have answered all these questions yourself by looking at the manuals.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
}
Post Reply