evibesmusic
asked on
Can you retrieve the value of an auto-increment column during MySQL insert?
Experts,
If I have a table which has 3 columns (ID, firstname, lastname), where 'ID' is an auto-increment data type is there any way to retrieve the auto-increment value during row creation?
Considering the following statement:
INSERT INTO table (firstname, lastname) VALUES ('Bob', 'Roberts');
Can I find out what the value of 'ID' is when the statement above is executed?
Cheers!
Is there any way to get th
If I have a table which has 3 columns (ID, firstname, lastname), where 'ID' is an auto-increment data type is there any way to retrieve the auto-increment value during row creation?
Considering the following statement:
INSERT INTO table (firstname, lastname) VALUES ('Bob', 'Roberts');
Can I find out what the value of 'ID' is when the statement above is executed?
Cheers!
Is there any way to get th
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regret to say I don't use PHP so I cannot verify that.
& as I'm not a PHP person I would wonder:
What guarantees that no other sql is being executed between $query and $last_id? (assuming multi-user access)
& as I'm not a PHP person I would wonder:
What guarantees that no other sql is being executed between $query and $last_id? (assuming multi-user access)
ASKER
@PortletPaul:
I understand the concern.
I am using the InnoDB DB type so I know that row level locking is supported but, that doesn't necessarily prevent a dirty read I guess?
This may not be a solid programming practice but, I'd still like to know if my code above would work?
I understand the concern.
I am using the InnoDB DB type so I know that row level locking is supported but, that doesn't necessarily prevent a dirty read I guess?
This may not be a solid programming practice but, I'd still like to know if my code above would work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@All:
Thank you for your answers.
Thank you for your answers.
ASKER
Thank you.
So using PHP I would assume that the following would place the last auto-increment value into my desired session variable?
$sql = "INSERT INTO table (firstname, lastname) VALUES ('Bob', 'Roberts')";
$query = mysql_query($sql) or die ("Could not run query: " . $sql . "<br />\n" . mysql_error () );
$last_id = mysql_insert_id();
$_SESSION['last_id'] = $last_id;