PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > Not getting last insert id
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Not getting last insert id

Réponse
 
LinkBack Outils de la discussion
Vieux 22/04/2006, 09h50   #1
saifmsg@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Not getting last insert id

Hello everyone,

I am using a custom php class where i have wrapped all the mysql
connection settings and query functions. Instead of calling direct
mysql functions i creat an instance of this class and than use the
custon functions for query, connection etc. At some places i also use
direct php mysql functions to connect and query.

My problem is that on my web site sometime i dont get last insert id of
an insert query. Is it because connections are shared or is there some
other problem. If i force mysql to always open a new connection for
query will it overload my server. Can anyone give me any solutions.

All suggestions are welcomed.

Thanks.

  Réponse avec citation
Vieux 22/04/2006, 23h55   #2
Aggro
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Not getting last insert id

saifmsg@gmail.com wrote:

> I am using a custom php class where i have wrapped all the mysql
> connection settings and query functions. Instead of calling direct
> mysql functions i creat an instance of this class and than use the
> custon functions for query, connection etc. At some places i also use
> direct php mysql functions to connect and query.
>
> My problem is that on my web site sometime i dont get last insert id of
> an insert query. Is it because connections are shared or is there some
> other problem. If i force mysql to always open a new connection for
> query will it overload my server. Can anyone give me any solutions.
>
> All suggestions are welcomed.


Are you calling the insert and "get last id" within the same connection?

"These functions are connection-specific, so their return values are not
affected by another connection which is also performing inserts."

http://dev.mysql.com/doc/refman/5.0/...increment.html
  Réponse avec citation
Vieux 23/04/2006, 02h05   #3
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Not getting last insert id

saifmsg@gmail.com wrote:
> My problem is that on my web site sometime i dont get last insert id of
> an insert query. Is it because connections are shared or is there some
> other problem. If i force mysql to always open a new connection for
> query will it overload my server. Can anyone give me any solutions.


I can't tell for certain because I haven't seen your code. But here's
my educated guess about the explanation:

I assume you're using PHP in an Apache environment, and each Apache
worker thread has its own persistent connection to the MySQL database.

The value returned by LAST_INSERT_ID() in MySQL is available only in the
scope of the database connection in which the insert was done that
created that ID.

The tricky thing is that subsequent HTTP requests (that is, page loads)
are not guaranteed to be handled by the same Apache worker thread.

So if you're inserting a record in one PHP request, and then in a
subsequent PHP request expecting to use LAST_INSERT_ID() to fetch that
value, you might not be able to.

Forcing a given Apache worker thread to handle subsequent requests for a
particular web client is called "session affinity" and it is not a
typical function of Apache. Though you might be able to find a patch
for it, for instance http://www.tfarmbruster.com/fcgi_sa.htm.

The best solution is to use the LAST_INSERT_ID() immediately, during the
same PHP page that inserted the record originally.

Regards,
Bill K.
  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 11h02.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09502 seconds with 11 queries