Search code examples
mysqlauto-incrementsql-insertmysql-insert-id

MySQL: Get AUTO_INCREAMENTED sequence number after Insert?


I've MySQL Table with Primary key as AUTO_INCREAMENTED (UNSIGNED INT) as sequence number. Every Time I insert new row in this table, I want to give acknowledgement to the user showing inserted AUTO_INCREAMENTED UNSIGNED INT.

Currently after inserting row, I’m executing SELECT Query to get last inserted row. Is there any smarter way to get inserted value, immediately after INSERT (without SELECT) ?


Solution

  • Try with mysql_insert_id()

    echo mysql_insert_id();
    

    Refer THIS for more about mysql_insert_id()

    And Try to avoid mysql_* statements due to the entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.

    There are two other MySQL extensions that you can better Use: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.

    In mysqli_* it will be like

    echo mysqli_insert_id();