473,508 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Next Autoindex

Hi guys, which query should I do to get it?
thx to all, chr
Jul 23 '05 #1
4 7171
Christian Giordano wrote:
Hi guys, which query should I do to get it?


There's a SQL function LAST_INSERT_ID().
http://dev.mysql.com/doc/mysql/en/in...functions.html

There's also a MySQL API call, mysql_insert_id().
http://dev.mysql.com/doc/mysql/en/mysql-insert-id.html

Regards,
Bill K.
Jul 23 '05 #2
> There's a SQL function LAST_INSERT_ID().
http://dev.mysql.com/doc/mysql/en/in...functions.html


I've already tried with this, but it returns 0 and I don't know how to
tell it in which table to get the number, so I used "SHOW TABLE STATUS"
and then I look for my data :S

thx, chr
Jul 23 '05 #3
Christian Giordano wrote:
There's a SQL function LAST_INSERT_ID().
http://dev.mysql.com/doc/mysql/en/in...functions.html


I've already tried with this, but it returns 0 and I don't know how to
tell it in which table to get the number, so I used "SHOW TABLE STATUS"
and then I look for my data :S


"LAST_INSERT_ID() ...
The last ID that was generated is maintained in the server on a
per-connection basis. This means the value the function returns to a
given client is the most recent AUTO_INCREMENT value generated by that
client."

If you connect to the database and query for LAST_INSERT_ID before you
do any inserts or updates that would cause an id to be generated, then
yes, it would be zero. The function only returns the last id generated
_during your current client session_.

I suspect that if you're trying to get the last insert id without having
inserted anything, you're not using it for its intended purpose. The
intended purpose is to make it easier to insert rows in dependent tables
that reference a row you inserted into your primary table:

INSERT INTO myMasterTable (val1, val2) VALUES (123, 456); /* autoinc
field is not named, thus gets a new incremented value */
INSERT INTO myDependentTable (fKey, val3, val4) VALUES
(LAST_INSERT_ID(), 789, 'abc');

This works because the last insert id is isolated to your current client
connection. Even if someone else is working on the database
concurrently and does their own insert to myMasterTable between the two
inserts you do, you still get the correct results.

But the purpose of LAST_INSERT_ID() is not for querying the greatest
value in an autoincrementing column.

Regards,
Bill K.
Jul 23 '05 #4
On Thu, 3 Mar 2005 09:48:30 +0000 (UTC), in mailing.database.mysql
Christian Giordano <ch*******@urmomlikesspam.com> wrote:
| > There's a SQL function LAST_INSERT_ID().
| > http://dev.mysql.com/doc/mysql/en/in...functions.html
|
| I've already tried with this, but it returns 0 and I don't know how to
| tell it in which table to get the number, so I used "SHOW TABLE STATUS"
| and then I look for my data :S
|
| thx, chr


Don't close the connection to the table before calling this function
i.e.

(JScript)
db = Server.CreateObject('ADODB.Command');
db.ActiveConnection = MM_CONNECTION_STRING;
db.CommandText = "INSERT INTO myTable ........";
db.Execute();

//--- get id of last value inserted into database
db.CommandText = "SELECT LAST_INSERT_ID()";
evdID = db.Execute().Fields(0).Value;

db.ActiveConnection.Close();

---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3038
by: Marcel | last post by:
Hello, I'm working on a search application for my website. The website contains a lot of pictures, and a search should return clickable thumbnails. No problems there. My problem started when I...
2
1491
by: Bengt Richter | last post by:
Is this a well known bug that's been fixed? I couldn't find any discussion of it, but maybe my googling's off today ;-/ >>> def foo(): ... it = iter(range(10)) ... while True: ... ...
7
2482
by: simonwittber | last post by:
>>> gen = iterator() >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next <method-wrapper object at 0x009D1BB0> >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next...
2
1742
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
9
3494
by: Steven D'Aprano | last post by:
I'm looking for some way to get the next floating point number after any particular float. (Any mathematicians out there: I am aware that there is no "next real number". But floats are not real...
13
14701
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
7
21899
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
4
3749
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
0
24156
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
2
19291
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
7225
marktang
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,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
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,...
1
5053
isladogs
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...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.