[Solved] ID of last inserted record.

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
Nicoza
Forum Newbie
Posts: 11
Joined: Mon Oct 02, 2006 11:43 am

[Solved] ID of last inserted record.

Post by Nicoza »

I suppose I can't keep on saying: "I'm new, please excuse me.... bla bla bla."

Obviously I am doing this wrong. Could someone enlighten me please. Simple thing. I need to know the ID (autoincrement field) of the last inserted record. I'm connected succesfully, and have obtained all the relevant values:

Code: Select all

//Skryf data.
$SQL = "INSERT INTO STEMBUS VALUES ('','$titel', '$kategorie', '$DATUM', '$opsie1', '1', '$opsie2', '0', '$opsie3', '0', '$opsie4', '0', '$opsie5', '0', '$beskrywing', '$verwysing', '$AFFILIATE')";
			
$RESULT = mysql_query($SQL);
$LAST_ID = mysql_insert_id($RESULT);
					
if (!$RESULT)
{
	echo "<p><font face='Verdana' size='2'>Data kon nie geskryf word nie.</font></p>" . mysql_error();
	exit;
}
Thanks a lot.
Last edited by Nicoza on Mon Oct 23, 2006 11:29 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$LAST_ID in your script doesn't hold the value you're looking for?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that should work.

note that the resource argument is optional. I never use it...

try echoing out $LAST_ID and it should be your inserted id.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You might to run your error checking on the result before the call to last insert id. Just to make sure you have a result to get the last insert id of.
Nicoza
Forum Newbie
Posts: 11
Joined: Mon Oct 02, 2006 11:43 am

[Solved] ID of last inserted record.

Post by Nicoza »

Thanks guys for the speedy reply.

I recieved the "traditional weird" errors per say. But as recommended, I simply removed the resource and it works fine.
I wanted to recreate the error to print it here but I was to happy it works and do not want to go through the effort as many tables and live data are involved here.

For future reference, I would like to understand. I was thinking that:

Code: Select all

mysql_insert_id()
should somehow be "bound" to a query (therefore the resource indicator) such as, perhaps;

Code: Select all

$RESULT = mysql_query($SQL);
But, Ok, it works fine. Thanks a million.

Yeah sure, I have "result checking" but as recommended, will do some dubble checking to ensure the record was actually created.

Thanks all!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The PHP manual on [url=http://www.php.net/mysql_insert_id]mysql_insert_id()[/url] wrote:mysql_insert_id -- Get the ID generated from the previous INSERT operation
That means whatever the last insert operation resulted in, I think.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Everah wrote:
The PHP manual on [url=http://www.php.net/mysql_insert_id]mysql_insert_id()[/url] wrote:mysql_insert_id -- Get the ID generated from the previous INSERT operation
That means whatever the last insert operation resulted in, I think.
Yes, per connection.

Code: Select all

$dbConnection = mysql_connect(...);
$result = mysql_query(...)
$id = mysql_insert_id($dbConnection); // not $result
http://de.php.net/mysql_insert_id wrote:If the link identifier is not specified, the last link opened by mysql_connect() is assumed.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Aha interesting. Did not know that.
Thanks for the info :lol:
Post Reply