470,561 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,561 developers. It's quick & easy.

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.

Apr 22 '06 #1
2 2957
sa*****@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
Apr 22 '06 #2
sa*****@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.
Apr 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Steve Leferve | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.