Re: Getting last record ID created from DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Philip Thompson wrote:
> For auto increment values, you don't have to specify the id. For example:
> 
> INSERT INTO t_users (f_name, l_name, e_mail, b_date, pic)
> VALUES ('$f_name', '$l_name', '$e_mail', '$b_date', null);
> 
> Then to find the latest entry:
> 
> SELECT user_id FROM t_users ORDER BY user_id DESC limit 1;

This is not the cleanest way and some databases do not actually
increment auto id fields (e.g. they could fill in the blanks from
previous deletes etc.).

Much better is to use the function in the MySQL API to get the insert id
or if you really must use SQL, just run "SELECT LAST_INSERT_ID();" which
does much the same thing.

Col

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux