Hi,
I have a site with various parts which allow users to enter info via forms
which gets entered into various tables in a MySQL db (I'm using ASP, rather
than PHP).
I have an ecards bit, and when users send a card, their data is entered into
the db via something like:
INSERT vars into ecard_tbl....
I just read that I can use this to get the most recent id from the
autoincrement field in the table:
SELECT LAST_INSERT_ID
However, it doesn't seem to need a table name for that. What if exactly at
the same time another users enters something into my guestbook. That will
submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID' give
me that ID, and not the ID from the card submission?
Am I missing something very obvious here? I checked the MySQL docs but can't
see anything obvious.
Thanks
Jim 6 12660
jim wrote: However, it doesn't seem to need a table name for that. What if exactly at the same time another users enters something into my guestbook. That will submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID' give me that ID, and not the ID from the card submission?
Am I missing something very obvious here? I checked the MySQL docs but can't see anything obvious.
"The value of mysql_insert_id() is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients." http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
"Aggro" <sp**********@yahoo.com> wrote in message
news:fT**************@read3.inet.fi... jim wrote:
However, it doesn't seem to need a table name for that. What if exactly
at the same time another users enters something into my guestbook. That
will submit data to the same MySQL db, so won't the 'SELECT LAST_INSERT_ID'
give me that ID, and not the ID from the card submission?
Am I missing something very obvious here? I checked the MySQL docs but
can't see anything obvious.
"The value of mysql_insert_id() is affected only by statements issued within the current client connection. It is not affected by statements issued by other clients."
http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
This is why you might want to SELECT max(ID) FROM table to get the lastest
one.
Rich
Rich R wrote: This is why you might want to SELECT max(ID) FROM table to get the lastest one.
I interpret jim's issue that he wants to fetch the last insert ID within
the context of his current connection, and ignore potential other ID's
inserted within other connections.
The LAST_INSERT_ID() function works correctly in this case, because it
is limited to the scope of the current connection. The function returns
the same value when you call it soon after your last insert or a long
time after your last insert, even if other connections insert more
records in the meantime.
Regards,
Bill K.
"Bill Karwin" <bi**@karwin.com> wrote in message
news:cq*********@enews2.newsguy.com... Rich R wrote: This is why you might want to SELECT max(ID) FROM table to get the
lastest one.
I interpret jim's issue that he wants to fetch the last insert ID within the context of his current connection, and ignore potential other ID's inserted within other connections.
The LAST_INSERT_ID() function works correctly in this case, because it is limited to the scope of the current connection. The function returns the same value when you call it soon after your last insert or a long time after your last insert, even if other connections insert more records in the meantime.
Regards, Bill K.
Then maybe I am mistaken, too. I created two tables each having an
auto_increment columm. I did an insert into the first and LAST_INSERT_ID() =
1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I do
another insert into the first table and LAST_INSERT_ID() = 2. So I've lost
the last inserted value from the second table, which is still 1. These
inserts were done on the same connection, one right after another. . So the
only way I can get the last inserted value from the second table is to do a
max() on the column. Am I out in the weeds on this one?
Best regards,
Rich
Rich R wrote: Then maybe I am mistaken, too. I created two tables each having an auto_increment columm. I did an insert into the first and LAST_INSERT_ID() = 1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I do another insert into the first table and LAST_INSERT_ID() = 2. So I've lost the last inserted value from the second table, which is still 1. These inserts were done on the same connection, one right after another. . So the only way I can get the last inserted value from the second table is to do a max() on the column. Am I out in the weeds on this one?
Yes, that is the way LAST_INSERT_ID() functions. It returns the last ID
that was auto-generated for any table within the current database
connection. If you do another insert to a table (either to the same
table or to a different table), then LAST_INSERT_ID() doesn't return the
previously generated ID value, it returns the more recent one.
However, that is not the issue that the original poster was asking
about. He was asking about the case where a second connection to the
database does its own insert, and causes another ID to be generated.
LAST_INSERT_ID() returns different values in this case, regardless of
the sequence of inserts. In each connection, the function returns the
most recent ID auto-generated by an insert within that respective
connection.
LAST_INSERT_ID() handles the case that jim was posting about. Your two
posts so far have described different issues, unrelated to jim's posting.
Regards,
Bill K.
"Bill Karwin" <bi**@karwin.com> wrote in message
news:cq*********@enews4.newsguy.com... Rich R wrote: Then maybe I am mistaken, too. I created two tables each having an auto_increment columm. I did an insert into the first and
LAST_INSERT_ID() = 1. I do an insert into the second table and the LAST_INSERT_ID() = 1. I
do another insert into the first table and LAST_INSERT_ID() = 2. So I've
lost the last inserted value from the second table, which is still 1. These inserts were done on the same connection, one right after another. . So
the only way I can get the last inserted value from the second table is to
do a max() on the column. Am I out in the weeds on this one?
Yes, that is the way LAST_INSERT_ID() functions. It returns the last ID that was auto-generated for any table within the current database connection. If you do another insert to a table (either to the same table or to a different table), then LAST_INSERT_ID() doesn't return the previously generated ID value, it returns the more recent one.
However, that is not the issue that the original poster was asking about. He was asking about the case where a second connection to the database does its own insert, and causes another ID to be generated. LAST_INSERT_ID() returns different values in this case, regardless of the sequence of inserts. In each connection, the function returns the most recent ID auto-generated by an insert within that respective connection.
LAST_INSERT_ID() handles the case that jim was posting about. Your two posts so far have described different issues, unrelated to jim's posting.
Regards, Bill K.
Hi Bill,
I think I addressed the post correctly. Doesn't matter if it's one
connection or many. If you want the last ID assigned you must do a Max(ID).
Within 1 connection, auto_increment will change not based on tables, simply
adding 1 to the last one. So if I want the last auto_incremented EmployeeID
and I have done some inserts to Projects with an ID ->auto_increment, all
bets are off, even safer, there are no bets.
If the OP did this all inside a transaction with InnoDB, then I still
wouldn't trust it.
Regards,
Rich
Regards,
Rich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Phil Powell |
last post by:
$sql = 'INSERT INTO fs_usermetadata (' . substr(trim($cols), 0,
strrpos(trim($cols), ',')) .
') VALUES (' . substr(trim($values), 0, strrpos(trim($values),
',')) . ')';
if (!mysql_query($sql))...
|
by: guitarromantic |
last post by:
Hey everyone.
I'm re-writing a php Content Management System (based loosely on
phpNuke but specific to my site). My first major change was attempting
to normalise the Review section database...
|
by: Terri |
last post by:
The following query will give me the top ten states with the most callers.
SELECT TOP 10 Count(Callers.CallerID) AS CountOfCallerID, Callers.State
FROM Callers
GROUP BY Callers.State
ORDER BY...
|
by: |
last post by:
Is a SELECT TOP query allowed in filtering rows from a
datatable? If it is not, is there any other easy way to
filter a given number of rows from a datatable? Thanks a
lot for your help.
|
by: lwwhite |
last post by:
I've got a a datasheet subform that gets its data from a SELECT DISTINCT query. The data is not editable. When I remove the DISTINCT qualifier from the query, the data is editable, but of course I...
|
by: wizardry |
last post by:
Hello -
I'm recently a newbie to mysql, but have experiance in oracle, mssql, access, etc...
Here is my problem, i've created the table needed to test the audit procedures. the data element...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |