Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

last_insert_id()

FidelieFidelie Member Posts: 32
helo.
I wonder how the function last_insert_id() works .
First I wonder if u can somehow see wat table it was
generated in?

Also they say in mysql.com that the last
generated id is maintained in the server on a per-connection
basis. wat do they mean with a session? Is it when u logg in with :
"mysql -u -p " ?


And if u dont use the last_insert_id() and dont have another unique
identifier in your table : is there at all any way to find out
wat id has the row u just inserted??
or is the only 2 ways to find out this to either have 1 more unique
column except for the auto_increment or use the last_insert_id-function=

thanks for answer. sorry for being not so good in mysql
/Fidelie

Comments

  • JonathanJonathan Member Posts: 2,914
    : helo.
    : I wonder how the function last_insert_id() works .
    : First I wonder if u can somehow see wat table it was
    : generated in?
    :
    : Also they say in mysql.com that the last
    : generated id is maintained in the server on a per-connection
    : basis. wat do they mean with a session? Is it when u logg in with :
    : "mysql -u -p " ?
    :
    :
    : And if u dont use the last_insert_id() and dont have another unique
    : identifier in your table : is there at all any way to find out
    : wat id has the row u just inserted??
    : or is the only 2 ways to find out this to either have 1 more unique
    : column except for the auto_increment or use the last_insert_id-function=
    :
    : thanks for answer. sorry for being not so good in mysql

    LAST_INSERT_ID() works on a per connection and per table basis. That means that say you connect, do:-

    INSERT INTO tablename ...blah blah...

    And then do:-

    SELECT LAST_INSERT_ID() FROM tablename

    It will return the ID of the last record you inserted into the table tablename. It's useful when you've got the primary key as an int auto_increment, and you want to know what the ID of the last item you inserted was.

    By per connection it means even if on another connection an INSERT happens before you call LAST_INSERT_ID(), you still get the ID of what YOU inserted with your connection.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • FidelieFidelie Member Posts: 32
    : : helo.
    : : I wonder how the function last_insert_id() works .
    : : First I wonder if u can somehow see wat table it was
    : : generated in?
    : :
    : : Also they say in mysql.com that the last
    : : generated id is maintained in the server on a per-connection
    : : basis. wat do they mean with a session? Is it when u logg in with :
    : : "mysql -u -p " ?
    : :
    : :
    : : And if u dont use the last_insert_id() and dont have another unique
    : : identifier in your table : is there at all any way to find out
    : : wat id has the row u just inserted??
    : : or is the only 2 ways to find out this to either have 1 more unique
    : : column except for the auto_increment or use the last_insert_id-function=
    : :
    : : thanks for answer. sorry for being not so good in mysql
    :
    : LAST_INSERT_ID() works on a per connection and per table basis. That means that say you connect, do:-
    :
    : INSERT INTO tablename ...blah blah...
    :
    : And then do:-
    :
    : SELECT LAST_INSERT_ID() FROM tablename
    :
    : It will return the ID of the last record you inserted into the table tablename. It's useful when you've got the primary key as an int auto_increment, and you want to know what the ID of the last item you inserted was.
    :
    : By per connection it means even if on another connection an INSERT happens before you call LAST_INSERT_ID(), you still get the ID of what YOU inserted with your connection.
    :
    : Hope this helps,
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :
    helo Jonathan
    Thank you .
    yes it is good help cause i was just wondering how to find out wat u just inserted without having any other unique field then the auto_incremented field.
    but are u really sure u can select from specific tables?
    cause it seems to me it always returns the last inserted id for all tables...
    or is it something u have to do to make this work by table?
    I m using mysql 4.0.14...

    Thank you again for patience.
    /Fidelie



  • FidelieFidelie Member Posts: 32
    : : helo.
    : : I wonder how the function last_insert_id() works .
    : : First I wonder if u can somehow see wat table it was
    : : generated in?
    : :
    : : Also they say in mysql.com that the last
    : : generated id is maintained in the server on a per-connection
    : : basis. wat do they mean with a session? Is it when u logg in with :
    : : "mysql -u -p " ?
    : :
    : :
    : : And if u dont use the last_insert_id() and dont have another unique
    : : identifier in your table : is there at all any way to find out
    : : wat id has the row u just inserted??
    : : or is the only 2 ways to find out this to either have 1 more unique
    : : column except for the auto_increment or use the last_insert_id-function=
    : :
    : : thanks for answer. sorry for being not so good in mysql
    :
    : LAST_INSERT_ID() works on a per connection and per table basis. That means that say you connect, do:-
    :
    : INSERT INTO tablename ...blah blah...
    :
    : And then do:-
    :
    : SELECT LAST_INSERT_ID() FROM tablename
    :
    : It will return the ID of the last record you inserted into the table tablename. It's useful when you've got the primary key as an int auto_increment, and you want to know what the ID of the last item you inserted was.
    :
    : By per connection it means even if on another connection an INSERT happens before you call LAST_INSERT_ID(), you still get the ID of what YOU inserted with your connection.
    :
    : Hope this helps,
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    helo again,
    sorry but also I wonder: If using this LAST_INSERT_ID do u no some good way to be sure u get the id u want. I mean even if u have it direct after the insertqvery maybe something goes wrong with the insert. do u have to make it inside some transaction thing so its always if insert fails it also dont take the LAST_INSERT_ID?
    im writing in java and send mysqlqveries with jdbc...
    do u no if u should check this in java or in mysql?
    thank you again
    /Fidelie



  • JonathanJonathan Member Posts: 2,914
    : but are u really sure u can select from specific tables?
    : cause it seems to me it always returns the last inserted id for all
    : tables...
    : or is it something u have to do to make this work by table?
    Sorry, I was wrong. I looked it up in the manual:-
    http://www.mysql.com/doc/en/Miscellaneous_functions.html
    It's actually the last inserted id, no matter what table it's from. Sorry 'bout that. Ah well, learnt something for myself too. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • JonathanJonathan Member Posts: 2,914
    : sorry but also I wonder: If using this LAST_INSERT_ID do u no some
    : good way to be sure u get the id u want. I mean even if u have it
    : direct after the insertqvery maybe something goes wrong with the
    : insert. do u have to make it inside some transaction thing so its
    : always if insert fails it also dont take the LAST_INSERT_ID?
    Really you should check that the INSERT query was successful before going any further anyway.

    : im writing in java and send mysqlqveries with jdbc...
    : do u no if u should check this in java or in mysql?
    Know nothing about JAVA, but in Perl's DBI, we can check if a query was successful by checking the return value of the do method (which executes a resultless query).

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.