geting last inserted row Id (MySQL)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

geting last inserted row Id (MySQL)

Post by alexus »

Hi
If I have INSERT command and I have id column in my table (autoincrement, primary key)
When I di inser i want to find out what is the ID, that db assidned for the row. .

what command should i use?

Thanks!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

take a look at mysql_insert_id()
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

just what i need, THANKS~
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post by Li0rE »

before I knew how to use insertid function (which was before I read your post) I just inserted all the information and queried

Code: Select all

$query = mysql_query("SELECT id FROM table WHERE something = '$somethingthatwasinsertedrightbeforethis'");
$row = mysql_fetch_assoc($query);
$id = $row['id'];
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

Li0rE, I was talking about INSERT not SELECT
Post Reply