|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello All,
Below is a snippet of code I use to add new users. Essentially after I add the new record I get back the userid which is an auto increment field. This code is written in Perl and run off of my web server. I'm paranoid. Paranoid that two people will run the "new user" scripts so close in time, that user B run the add part a micro second before user A. Is there a way to return the auto increment value in response to the "do"? Or maybe this is all moot? Maybe the return value for the auto increment value is per "logged into the database" session. And since technically each Perl script logged in on their own, there is no need to worry? Many thanks ahead, as always. $inserted_rows = $db->do ($add_msg); return 1 if($inserted_rows != 1); $dbHandle=$db->prepare("SELECT LAST_INSERT_ID()"); $dbHandle->execute(); return 2 if($dbHandle->rows() != 1); my @row = $dbHandle ->fetchrow_array; ....and then here I get and use the returned value....$row[0] |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Daniel Kaplan wrote:
> Hello All, > > Below is a snippet of code I use to add new users. Essentially after I add > the new record I get back the userid which is an auto increment field. This > code is written in Perl and run off of my web server. > > I'm paranoid. Paranoid that two people will run the "new user" scripts so > close in time, that user B run the add part a micro second before user A. > > Is there a way to return the auto increment value in response to the "do"? > Or maybe this is all moot? Maybe the return value for the auto increment > value is per "logged into the database" session. And since technically each > Perl script logged in on their own, there is no need to worry? > > Many thanks ahead, as always. > > > > $inserted_rows = $db->do ($add_msg); > return 1 if($inserted_rows != 1); > > $dbHandle=$db->prepare("SELECT LAST_INSERT_ID()"); > $dbHandle->execute(); > return 2 if($dbHandle->rows() != 1); > my @row = $dbHandle ->fetchrow_array; > > > ....and then here I get and use the returned value....$row[0] > > > LAST_INSERT_ID is based on the connection being used. As long as you use the same connection you will get the correct id. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:jMCdnU7H1IM8BOvanZ2dnUVZ_tbinZ2d@comcast.com. .. > LAST_INSERT_ID is based on the connection being used. As long as you use > the same connection you will get the correct id. Well here is where my lack of knowledge in the world of Network Administration comes up to bite me in the behind. Everything is on one server, Perl, MySQL, etc. The way the script (as well as the rest) does it is that when creating a new user it "logs into the database" adds the record, retrieves the LAST_INSERT_ID and then "logs out of the database". So as long each instance of the script being run counts as a separate connection, then the code as written is cool. If my lack of knowledge here is wrong, please let me know. Thanks again. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Daniel Kaplan wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message > news:jMCdnU7H1IM8BOvanZ2dnUVZ_tbinZ2d@comcast.com. .. > >> LAST_INSERT_ID is based on the connection being used. As long as you use >> the same connection you will get the correct id. > > Well here is where my lack of knowledge in the world of Network > Administration comes up to bite me in the behind. Everything is on one > server, Perl, MySQL, etc. > > The way the script (as well as the rest) does it is that when creating a new > user it "logs into the database" adds the record, retrieves the > LAST_INSERT_ID and then "logs out of the database". So as long each > instance of the script being run counts as a separate connection, then the > code as written is cool. > > If my lack of knowledge here is wrong, please let me know. Thanks again. > > > Before any script performs an operation on the database, it must connect to the database. Connections from other scripts do not work. This has nothing to do with network administration. It's straight programming. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Sat, 29 Dec 2007 12:34:45 -0500, "Daniel Kaplan"
<NoSPam@NoSpam.com> wrote: >Hello All, > >Below is a snippet of code I use to add new users. Essentially after I add >the new record I get back the userid which is an auto increment field. This >code is written in Perl and run off of my web server. > >I'm paranoid. Paranoid that two people will run the "new user" scripts so >close in time, that user B run the add part a micro second before user A. > >Is there a way to return the auto increment value in response to the "do"? >Or maybe this is all moot? Maybe the return value for the auto increment >value is per "logged into the database" session. And since technically each >Perl script logged in on their own, there is no need to worry? http://dev.mysql.com/doc/refman/5.1/...last-insert-id >Many thanks ahead, as always. > > > > $inserted_rows = $db->do ($add_msg); > return 1 if($inserted_rows != 1); > > $dbHandle=$db->prepare("SELECT LAST_INSERT_ID()"); > $dbHandle->execute(); > return 2 if($dbHandle->rows() != 1); > my @row = $dbHandle ->fetchrow_array; > > > ....and then here I get and use the returned value....$row[0] > -- ( Kees ) c[_] It is difficult to say what is impossible, for the dream of yesterday is the hope of today and the reality of tomorrow. (Robert Goddard) (#39) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Sat, 29 Dec 2007 14:21:45 -0500, Daniel Kaplan wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message > news:jMCdnU7H1IM8BOvanZ2dnUVZ_tbinZ2d@comcast.com. .. > >> LAST_INSERT_ID is based on the connection being used. As long as you use >> the same connection you will get the correct id. > > Well here is where my lack of knowledge in the world of Network > Administration comes up to bite me in the behind. Everything is on one > server, Perl, MySQL, etc. > > The way the script (as well as the rest) does it is that when creating a new > user it "logs into the database" adds the record, retrieves the > LAST_INSERT_ID and then "logs out of the database". So as long each > instance of the script being run counts as a separate connection, then the > code as written is cool. > > If my lack of knowledge here is wrong, please let me know. Thanks again. $db contains your connection. Other instances of the script or other scripts that have also opened connections do not own the same connection in $db. -- 19. I will not have a daughter. She would be as beautiful as she was evil, but one look at the hero's rugged countenance and she'd betray her own father. --Peter Anspach's list of things to do as an Evil Overlord |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
>Below is a snippet of code I use to add new users. Essentially after I add
>the new record I get back the userid which is an auto increment field. This >code is written in Perl and run off of my web server. > >I'm paranoid. Paranoid that two people will run the "new user" scripts so >close in time, that user B run the add part a micro second before user A. LAST_INSERT_ID() returns the last inserted ID *ON YOUR CONNECTION*. Other connections can insert until they are blue in the face and not change anything. It wouldn't be worth much if it didn't work this way. >Is there a way to return the auto increment value in response to the "do"? >Or maybe this is all moot? Yes, it is. >Maybe the return value for the auto increment >value is per "logged into the database" session. That's how it works. >And since technically each >Perl script logged in on their own, there is no need to worry? Correct, unless someone gets to re-write your Perl script. |
|
![]() |
| Outils de la discussion | |
|
|