Page 1 of 1

[Solved] ID of last inserted record.

Posted: Mon Oct 23, 2006 10:41 am
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.

Posted: Mon Oct 23, 2006 10:44 am
by volka
$LAST_ID in your script doesn't hold the value you're looking for?

Posted: Mon Oct 23, 2006 10:45 am
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.

Posted: Mon Oct 23, 2006 11:11 am
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.

[Solved] ID of last inserted record.

Posted: Mon Oct 23, 2006 11:24 am
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!

Posted: Mon Oct 23, 2006 12:02 pm
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.

Posted: Mon Oct 23, 2006 1:22 pm
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.

Posted: Mon Oct 23, 2006 5:46 pm
by batfastad
Aha interesting. Did not know that.
Thanks for the info :lol: