This is how i do the same thing as i belive you're asking.

$result = mysql_query("select max(id) from [table]");
$lastID = mysql_result($result, 0);

Hope this helps

                    "Cosby, Christopher"                                               
                    <Christopher.Cosby@s       To:     'Chris Payne' 
          >                 cc:                                     
                                               Subject:     RE: [PHP-DB] Last ID from 
                    06/26/2002 04:08 PM                                                

> -----Original Message-----
> From: Chris Payne [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, June 26, 2002 3:57 PM
> Subject: [PHP-DB] Last ID from database?
> Hi there everyone,
> How Can I grab just the LAST ID from a database in MySQL with PHP
> easily?

Which LAST ID? There are several meanings there.  I'll try a couple.

1) If the column is the only auto_increment column in a table, and you want
to know the value of that column after an INSERT, just use the following to
get the value that was inserted.
$dbh = mysql_connect($host, $user, $pass);
mysql_select_db($db, $dbh);
mysql_query("INSERT INTO table (id, col1, col2) VALUES ('','val1','val2')",
$lastid = mysql_insert_id($dbh);

2) Just want to get the highest value in a column?  Try this.
$dbh = mysql_connect($host, $user, $pass);
mysql_select_db($db, $dbh);
$result = mysql_query("SELECT MAX(columnname) FROM tablename", $dbh);
if ( $row = mysql_fetch_object($result) ) {
 $lastid = $row->columnname;

> Thanks you :-)
> Chris
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (
> Version: 6.0.372 / Virus Database: 207 - Release Date: 6/20/2002

    - - - - - - -  Appended by Scientific-Atlanta, Inc.  - - - - - - -
This e-mail and any attachments may contain information which is
confidential, proprietary, privileged or otherwise protected by law. The
information is solely intended for the named addressee (or a person
responsible for delivering it to the addressee). If you are not the
intended recipient of this message, you are not authorized to read, print,
retain, copy or disseminate this message or any part of it. If you have
received this e-mail in error, please notify the sender immediately by
return e-mail and delete it from your computer.

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to