mntruell
mntruell

Reputation: 181

Inserting Into a Row and Then Selecting from that Row in One Query

I am working on a web development project that requires PHP and a MYSQL database. I am a bit of a MYSQL newbie. In one php file, I am inserting a few data into a row of a table. This data alone cannot identify the row, but an auto-incremented key is automatically generated which can identify the row. Is there any way to insert this row and select from it in the same query so it doesn't have to be identified with WHERE statement? Any other solutions are very welcome.

Thanks, Michael

Upvotes: 0

Views: 73

Answers (2)

Cristian Abelleira
Cristian Abelleira

Reputation: 355

As an alternative you could get the last id with mysql_insert_id php function.

Note that this function is deprecated but you could find alternatives in the link below...

http://www.php.net//manual/en/function.mysql-insert-id.php

Upvotes: 0

Manjunatha
Manjunatha

Reputation: 44

Use this code
// insert a datarow, primary key is auto_increment

// value is a unique key $query = "INSERT INTO test (value) VALUES ('test')"; mysql_query( $query );

   echo 'LAST_INSERT_ID: ',
          mysql_query( "SELECT LAST_INSERT_ID()" ),
          '<br>mysql_insert_id: ',
          mysql_insert_id();

Upvotes: 1

Related Questions