PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Question about LAST_INSERT_ID()
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question about LAST_INSERT_ID()

Réponse
 
LinkBack Outils de la discussion
Vieux 29/12/2007, 17h34   #1
Daniel Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question about LAST_INSERT_ID()

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]


  Réponse avec citation
Vieux 29/12/2007, 19h01   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

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
==================

  Réponse avec citation
Vieux 29/12/2007, 19h21   #3
Daniel Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

"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.


  Réponse avec citation
Vieux 29/12/2007, 20h04   #4
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

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
==================

  Réponse avec citation
Vieux 29/12/2007, 20h25   #5
Kees Nuyt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

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)
  Réponse avec citation
Vieux 29/12/2007, 20h32   #6
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

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
  Réponse avec citation
Vieux 29/12/2007, 23h14   #7
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about LAST_INSERT_ID()

>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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15775 seconds with 15 queries