Home > MySQL, PhP, Tech > Get current AUTO_INCREMENT value – MySQL query

Get current AUTO_INCREMENT value – MySQL query

January 29th, 2009 Leave a comment Go to comments

Solution is simple, use mysql_insert_id() php function.

mysql_insert_id() returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement. Use this function after you have performed an INSERT statement into a table that contains an AUTO_INCREMENT field, or have used INSERT / UPDATE to set a column value with LAST_INSERT_ID(expr).

mysql_insert_id() returns 0 (zero) if the previous statement does not use an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the statement that generates the value – like:  mysql_query($query) .

  1. No comments yet.
  1. No trackbacks yet.
GoCache - ByREV-Cache v1.0 - live served in : 0.157955 sec (gzip)