Subject RE: [firebird-php] auto increment last insert
Author Johan van Zyl
Hi Nige
If I may call you that?<g> (I am very apprehensive in this NG after some
hostile reaction, I am still consulting with with my analyst about this<g>)
Thx for this very detailed response - I really appreciate it!
I notice that you did not tell me to just RTFM, and nothing else!
That was very mature of you<g> It seems that the trend is in the opposite
direction. PITY!
We are all supposed to be in the same boat - and all of us learning from one
another? Apparently not<g>
As it is late, it is now Thursday here already, I will study this later
today!
Thx once again for the answer, and THX for staying in the SPIRIT of it all!
Greetings

JVZ
-----Original Message-----
From: Nigel Weeks [mailto:nweeks@...]
Sent: 25 November 2004 00:01
To: firebird-php@yahoogroups.com
Subject: RE: [firebird-php] auto increment last insert


I've just arrived in the office - did this thread get completed?
Is the original poster satisfied with the information?

Basically, a set-by-step approach to replacing MySQL's autoinc, without
procedures, triggers, and trying to get the value after the insert(could
be
inaccurate if the generator's been used since the insert...)

This example will use the following simple table:
CREATE TABLE tbl_test (
int_ID NUMERIC(18,0) NOT NULL,
str_name VARCHAR(100),
PRIMARY KEY(int_ID)
);
/* This is the generator for the tbl_test table */
CREATE GENERATOR gen_tbl_test;

------
Now, into PHP land
------

<?php
// Open up the connection
$conn = ibase_connect("server:/path/to/db.fdb","sysdba","masterkey");

// in php4, run a query to get the next value of the generator
$gensql = "SELECT GEN_ID(gen_tbl_test,1) AS NEW_ID from rdb\$database";
/* Notes:
* I renamed the result as NEW_ID so it's easier to see amongst the rest
of
the code
* Also, remember to escape the $ in the rdb/$database - otherwise PHP
will
think it's a variable
*/

// Run the query, and put the resulting recordset into a variable with a
nice name
$genrec = ibase_query($gensql);

// Fetch the value from the recordset
$genobj = ibase_fetch_object($genrec);

/* We now have the next value for the table. We can now
* use it in normal insert statements, just like any other variable
*/

// Prepare an insert statement
$inssql = "INSERT INTO tbl_test (int_id, str_name) VALUES
($genobj->NEW_ID,
'Romulus of Constantinople')";
// Run the insert query
$insrec = ibase_query($inssql);

?>

------
If you remove all the comments, it's about 6 lines of code.
As a previous message illustrated, you could wrap up the generator fetch
section into a small function, which I think is a very nice idea!

I hope this info is what you're after! ;-)

Nige.




Yahoo! Groups Sponsor
ADVERTISEMENT





----------------------------------------------------------------------------
--
Yahoo! Groups Links

a.. To visit your group on the web, go to:
http://groups.yahoo.com/group/firebird-php/

b.. To unsubscribe from this group, send an email to:
firebird-php-unsubscribe@yahoogroups.com

c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.799 / Virus Database: 543 - Release Date: 2004/11/19


[Non-text portions of this message have been removed]