Subject | RE: [firebird-php] auto increment last insert |
---|---|
Author | Nigel Weeks |
Post date | 2004-11-24T22:01:17Z |
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.
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.