views:

57

answers:

2

Hello,

in MySQL, I have table. One column has auto-increment. When I insert something

mysql_query('INSERT INTO `table` SET `column` = "data";');

Now, what I want is, without further queries, to get the value of the auto-incrementing column, maybe like this:

mysql_query('insertion = (INSERT INTO `table` SET `column` = "data"); 
SELECT `auto_incrementing_column` FROM `table`.insertion');

Please tell me how something like that is done. Thanks in advance!

+2  A: 

Can you use LAST_INSERT_ID()? Check out this link: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

Abe Miessler
how? (I am not very experienced... ) SELECT `auto_incrementing_column` FROM `table`.LAST_INSERT_ID();?
arik-so
Just put SELECT LAST_INSERT_ID() right after you do the insert.
Abe Miessler
+3  A: 

I think the best you can do is after the insert, do this:

SELECT LAST_INSERT_ID();

I don't know of a way to do it with one query.

UPDATE Actually, it appears there is a multi_query command you could use. So you could pass both queries to $mysqli->multi_query (separate each query with a semicolon). That would save you an extra round trip to the server.

dcp
Still it is perfectly safe (http://dev.mysql.com/tech-resources/articles/storage-engine/part_3.html). Btw, couldn't you send both SLQs as one string to mysql_query()?
Unreason
@Unreason - See my update, it appears PHP has a command to allow you to send multiple queries, which would save an extra round trip to the server.
dcp