Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

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 Dev Shed Forums Sponsor:
  #1  
Old August 8th, 2005, 09:13 AM
neobuddah's Avatar
neobuddah neobuddah is offline
cosmos curator
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2002
Location: Leeds, UK
Posts: 678 neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 14 h 17 m 49 sec
Reputation Power: 8
Get last inserted id?

Hi all

I'm hoping to start development with PHP and Firebird on a new project. I've been running some tests, but can't seem to find a way to grab the id of the last INSERTed row.

Can anyone help me?
__________________
R.T.F.M - Its the only way to fly...

"No matter what you do, or how good it is, someone will always ask for more features. Or to change the colour of something, then change their minds."

Personal:
experience// 8 Years Web Development
technologies// Standards-compliant, valid, & accessible (x)HTML/CSS, XML/XSL/XPath/XQuery/XUpdate, (OOP) PHP/(My)SQL, eXist/Xindice/XMLDBs
packages// Photoshop, Illustrator, Flash/Fireworks/Director
environment// FC2, MySQL, Lighttpd, PHP5, Mojavi/Agavi
site// //refactored.net/ (Coming soon...)
quote// Programming is the eternal competition between programmers who try to make apps more and more idiot proof and the universe that makes dumber idiots. So far, the universe is winning...

Reply With Quote
  #2  
Old August 9th, 2005, 03:33 AM
neobuddah's Avatar
neobuddah neobuddah is offline
cosmos curator
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2002
Location: Leeds, UK
Posts: 678 neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level)neobuddah User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 14 h 17 m 49 sec
Reputation Power: 8
Is there no way to obtain this then?

Reply With Quote
  #3  
Old August 9th, 2005, 03:41 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 19 sec
Reputation Power: 8
Try with:

SELECT gen_id(gen_name, 0) from rdb$Database

where gen_name is generator holding value you need.

P.S. I had a problem posting here, so sorry for delay ;-)

--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer

* Firebird Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org

Reply With Quote
  #4  
Old August 11th, 2005, 05:08 AM
upscene upscene is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 223 upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 7 h 14 m 50 sec
Reputation Power: 8
Although the proposed solution works for single-user systems, I would recommend against it for just that reason.

GEN_ID(generatorname, 0) will return the current value of the generator. This does not have to be the value you just used in the trigger. If another user did the same, you will get that value instead.


The best way is to fetch the value first, then use the value for subsequent inserts.

SELECT GEN_ID(mygenerator, 1) AS NEW_VALUE FROM RDB$DATABASE

INSERT INTO ... VALUES (NEW_VALUE, ...)


With regards,

Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com
__________________
Martijn Tonies
Database Workbench: developer IDE for Firebird, MySQL, InterBase, MSSQL Server and Oracle
Upscene Productions
http://www.upscene.com

Reply With Quote
  #5  
Old August 9th, 2006, 04:09 AM
gonzaloko gonzaloko is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 gonzaloko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 21 sec
Reputation Power: 0
Doesn't work for me. SELECT GEN_ID(mygenerator, 1) AS NEW_VALUE FROM RDB$DATABASE increases the value of generator. when I insert the value of generator is one more.

this is the code i use in my procedure to return last id inserted.

INSERT INTO ... VALUES (NEW_VALUE, ...)
SELECT GEN_ID(mygenerator, 0) FROM RDB$DATABASE INTO NEW_VALUE

(NEW_VALUE is an output variable)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Get last inserted id?


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 2 hosted by Hostway
Stay green...Green IT