Is there a way to return the id of a string that was just created in MySQL with PHP?

When I create a new record in my MySQL database, I would like the query to return the identifier of the table just created. If he cannot do this, is there any other way to find out this identifier? The reason I want to do this is to associate a new line with the line that spawned it (and there is no guarantee that they are consistent)

+4
source share
6 answers

Do you mean the last auto-increment identifier?

<?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('mydb'); mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf("Last inserted record has id %d\n", mysql_insert_id()); ?> 
+11
source

Do you want mysql_insert_id()

+6
source

You mean the identifier of the record you just inserted into the table if you are looking for mysql_insert_id .

+2
source
+2
source

Use mysql_insert_id function ... http://php.net/mysql_insert_id

Sorry that underscores convert the "insert" into italics ... follow the link and you should be fine.

+1
source

You have LAST_INSERT_ID () in My SQL (@@ IDENTITY in ms sql) and mysql_insert_id in php.

Personally, however, I try to avoid auto-increments / identity columns and generate my id in other ways. Among other reasons, because they make inserting into multiple tables more difficult.

/ B

0
source

All Articles