Eugeny89
Eugeny89

Reputation: 3731

PHP: inserting a row and selecting id of inserted row

After I inserted a row I want to know it's id. Is it possible to get it in the same query with insert? Note that I'm facing the problem on PHP

Upvotes: 0

Views: 68

Answers (3)

Sliq
Sliq

Reputation: 16494

or, better, for mysqli/oop:

$mysqli->query("INSERT ...");
echo $mysqli->insert_id;

http://php.net/manual/en/mysqli.insert-id.php

Upvotes: 1

MrCode
MrCode

Reputation: 64526

Using the mysql_* functions:

// after the query
$id = mysql_insert_id();

Using MySQLi

// after the query
$id = $mysqli->insert_id;

Using PDO

// after the query
$id = $pdo->lastInsertId();

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838066

Yes, you can use mysql_insert_id

mysql_insert_id — Get the ID generated in the last query

Upvotes: 3

Related Questions