PEAR Packages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPEAR Packages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 1st, 2005, 09:33 PM
booyau booyau is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 booyau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PearDB question I'm sure has been asked a million times

I have a question regarding Pear:B inserts that I'm sure has been asked a million times, but I haven't been able to find any answers by googl'ing around.

When I do an insert into the database in Pear:B, how do I find out the ID/Key of the newly inserted row? Do I really have to do a new select statement with all of the values that I inserted with?

I noticed there is a function under Pear DB Dataobject, but I'd rather not bring another package into the mix...

Thanks in advance,
Jason

Reply With Quote
  #2  
Old August 1st, 2005, 10:29 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,729 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 1 h 32 m 7 sec
Reputation Power: 6
RE: PearDB question I'm sure has been asked a million times

Well if you are using MySQL you can use LAST_INSERT_ID() though that is rather DB specific.

Quote:
You can find the value used for an AUTO_INCREMENT column by using the LAST_INSERT_ID() function.


If you want to be as portable as possible - how about selecting the MAX(ID)?

Reply With Quote
  #3  
Old August 2nd, 2005, 03:04 AM
booyau booyau is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 booyau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: PearDB question I'm sure has been asked a million times

Hmm... is there any more efficient method (read: one not involving another database call)? Also, if the site is under heavy traffic, don't race conditions become an issue? What if one user does something that induces an INSERT, and another user's PHP thread does another INSERT before the first user's PHP thread selects the last inserted ID?

Reply With Quote
  #4  
Old August 2nd, 2005, 03:10 AM
booyau booyau is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 booyau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: PearDB question I'm sure has been asked a million times

I found the answer to my own question, although only after finding about last_insert_id and googling arond ont hat for abit:

"The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behaviour ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions."

Reply With Quote
  #5  
Old August 2nd, 2005, 03:11 AM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,729 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 1 h 32 m 7 sec
Reputation Power: 6
RE: PearDB question I'm sure has been asked a million times

Any method call will involve another DB call (What do you think the method does). As for the race part - yeah you can have issues with it (though how much of one I don't know - so see the docs). How much traffic are you expecting?

Reply With Quote
  #6  
Old August 2nd, 2005, 12:48 PM
booyau booyau is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 booyau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: PearDB question I'm sure has been asked a million times

From the docs, and the fact that the last insert id is remembered on a per-connection basis, it looks like race conditions may be eliminated entirely here.

As for how much traffic, I don't know exactly, but we're talking about a production web site with potentially hundreds of concurrent users.

Reply With Quote
  #7  
Old August 24th, 2005, 07:06 AM
sjors sjors is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Utrecht, Netherlands
Posts: 1 sjors User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: PearDB question I'm sure has been asked a million times

There is an example of this method on:
http://laughingmeme.org/archives/001701.html

In the discussion following this example it is suggested to use the PEAR method nextId().

Reply With Quote
  #8  
Old May 8th, 2006, 09:33 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: PearDB question I'm sure has been asked a million times

alternatively you can switch to PEAR::MDB2 which provides a lastInsertID() method for this purpose ..

regards,
Lukas

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPEAR Packages > PearDB question I'm sure has been asked a million times


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT