Get last insert ID from Access - php

I am familiar with the MySQL function LAST_INSERT_ID; is there a similar function for performing the same query with a MS Access database via ODBC?
In my specific case, I am using PHP+PDO to insert rows into an Access database, and would like to know the last primary key value of each insert as they are performed.
If this functionality is not available, are there any alternatives? (without changing the database)
Thank you.

It seems that Access 2000 or later supports the ##IDENTITY property. So, you would only need to select its value after an INSERT:
select ##IDENTITY from myTable
Please see the MSDN link: Retrieving Identity or Autonumber Values
In short:
[...] Microsoft Access 2000 or later does support the ##IDENTITY property to retrieve the value of an Autonumber field after an INSERT. Using the RowUpdated event, you can determine if an INSERT has occurred, retrieve the latest ##IDENTITY value, and place that in the identity column of the local table in the DataSet.

As others have said, SELECT ##IDENTITY works with Jet 4 and the ACE.
A new consideration has been introduced with Access 2010, and that's because the new ACE version supports table-level data macros, which are the equivalent of triggers. Thus, an insert in one table might trigger an insert in another, so that ##IDENTITY might be the value for the second table instead of the top-level one. So far as I know, there is no equivalent to SQL Server's SCOPE_IDENTITY() for this scenario.
I have asked about it in other Access forums and nobody seems to know. It's something to watch for should you be using an ACCDB with table-level data macros.

I've never attempted to use access with php, but two ideas come to mind, The first one is simple. And that's to simple select max(id) from table after your insert, since it is auto incrementing you will get the highest value which should be the insertted value. Secondly you can try using odbc_cursor (http://au2.php.net/manual/en/function.odbc-cursor.php).

Try running "SELECT ##IDENTITY FROM MyTable" after your insert.

Related

PHP PDO Firebird inserting

I'm new in Firebird but I'd like to write a small script in PHP that reads a CSV file and fills an existing Firebird db with its data.
The problem is I don't really know how to use the autoincrement generator. I've googled a lot but it's still a mistery for me. There is a gen_main generator defined in the db and I can use it in the IBExpert's query builder but cannot in PHP...
I saw a function named ibase_gen_id, what is the "PDO-way" of it?
What is the process of inserting a row that has an autoincremented field with PDO?
Thanks in advance!
NOTE: I have never used PDO, so I can't comment on PDO specifics.
Depending on your exact needs you can use: NEXT VALUE FOR
NEXT VALUE FOR <sequence-name>
or GEN_ID
GEN_ID(<sequence-name>, 1)
To get the next value of the sequence/generator.
You can either use these directly in your INSERT statement, or first issue a SELECT query against RDB$DATABASE to retrieve the value yourself before inserting: in Firebird you need to use a SELECT to retrieve values, and you always need to select against a table. RDB$DATABASE is guaranteed to contain only one row (like Oracle's DUAL).
So you need SELECT NEXT VALUE FOR GEN_MAIN FROM RDB$DATABASE or SELECT GEN_ID(GEN_MAIN, 1) FROM RDB$DATABASE to get the next sequence value.
In general however I would advise you to add a trigger to do the auto-increment for you, see Firebird Generator Guide for details. You can then use INSERT ... RETURNING <column-list> to retrieve the generated id.

PHP MySQL foreign-key auto insert

So basically what I am trying to do is when a user of my site creates a new account on our register page, I'd like the primary key from the newly created row on the User table (basic info table, email, password, etc.) to be inserted into a new row on the Profile table (more descriptive info, about me, display name, etc.)
I'd like to do this in PHP and any help would be appreciated.
if you are using mysqli look at:
http://www.php.net/manual/en/mysqli.insert-id.php
Get the id after your first insert and then use this in your next insert.
If doing it "in php" isn't really a requirement, then you can use MySQL's built in Trigger mechanism to do this update.
Triggers cause something to happen AFTER or BEFORE an event(INSERT, UPDATE,DELETE)
So your trigger would be:
CREATE TRIGGER thistrigger AFTER INSERT
ON User FOR EACH ROW
UPDATE PROFILE SET "whatever"
On Triggers: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I think there isn't a really elegant way in MySQL, basically because INSERT doesn't return anything. PostgreSQL does allow for an INSERT ... RETURNING clause, but that's an extension.
That said, if you're using the mysql_* functions in PHP, you can use mysql_insert_id, which might suffice for your needs (i.e. if your primary key is an AUTO INCREMENT integer).
If you are using a mysql database, you could alternatively do another query call from php with the following query:
"SELECT LAST_INSERT_ID();"
More info about it here: http://www.jpgtutorials.com/mysql-last_insert_id-function
It is connection specific. Concurrent inserts from different connections won't affect the current connection.

How to display an incremental value that was just created in mysql

I have a PHP script that creates an entry into a mysql database. The PHP inserts all of the data except the primary key, which mysql automatically increments. The problem is that i want to insert information into two tables, and these tables must be able to associate. Is there a way to have PHP create an entry in one table in mysql, then figure out the incremental primary key value from that first table, in order for it to insert into the second mysql table as a reference?
Yes. This is certainly doable. The function/method you use to get the auto-incremented value that was just inserted will depend on the way you access MySQL from PHP. If you're using the mysql_ functions, use mysql_insert_id(). If you're using the mysqli_ functions (or OO versions), use mysqli_insert_id(). If you're using PDO, use PDO::lastInsertId(). In all cases, the function delegates to the MySQL function last_insert_id() which is local to the connection so you need not worry about concurrent threads interfering with each other.
You can use:
mysql_insert_id()
This will return the id of the previous query. See below:
http://php.net/manual/en/function.mysql-insert-id.php
To get the unique ID inserted for the first table you can use LAST_INSERT_ID() and save that as reference for your second table:
SELECT LAST_INSERT_ID();
Use this as another option I suppose. For PHP you can use mysql_insert_id as suggested :)

how to prevent from getting wrong ID

database= mysql language=php
I am coding program to work like this
PC1 =computer1
step
1.PC1 insert data ,new ID from Auto-increment.
2.PC1 select last ID
everything work fine but..
The problem when your code is used by many computers at the same mili-sec.
For example
PC1insert data,Auto-increment new ID
2.PC2 insert data ,Auto-increment new ID
3.PC1 select last ID <-------Wrong
4PC2 select last ID
How to config database or modify php code to prevent this , thankyou.
You should use mysql_insert_id (or the equivalent call for the API you are using) to get the ID of the last inserted row.
I imagine that now you are doing this:
SELECT MAX(id) FROM yourtable
This won't work unless you use transactions. But using the function mysql_insert_id is the better way.
In the documenation there is also a caution and some notes that you should read:
Caution: mysql_insert_id() will convert the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT (64 bits) the conversion may result in an incorrect value. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.
Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.
in mysql if you call
SELECT LAST_INSERT_ID();
you will get the last auto generated id fro the current connection, not for the whole server. So if another record is created in another connection it wont effect the result returned by LAST_INSERT_ID().
MySQL will return the correct last-inserted ID right after any INSERT statement per client (computer) connection, so you won't have to worry about that; the server won't mix them up. From the documentation:
Each client will receive the last inserted ID for the last statement that client executed.
As long as you call mysql_insert_id() in your PHP code to retrieve the insert ID, there's nothing to worry about.
mySQL has the LAST_INSERT_ID() function for that.
The PHP equivalent (if you use the classical mysql functions) is mysql_insert_id() with the exception that this function should be called immediately after the INSERT query because it acts on the last query made.
These functions work on a per-connection basis, it will not be influenced by records inserted by other clients.

PHP/Oracle, returning inserted id's from Merge Into

I am currently working on a PHP project with an Oracle database. To update a table, the php code I'm working with uses a SQL "MERGE INTO" method to loop through a table and see if values for multiple records exist in another table. If they don't exist yet, the values are inserted into my table. If the values already exist, nothing happens.
I would like to have another query run after this that uses the auto incremented id's created in the MERGE INTO query. Is there a way to get an array of the newly created ids? I was hoping for something like mysql_insert_id, but I haven't found anything like that yet.
Thanks!
Oracle has supported the MERGE syntax since 9i. Haven't tried, but you might be able to use the RETURNING clause on the MERGE statement...
Oracle uses sequences for handling automatically incremented values. Once you've created a sequence, you can use:
sequence_name.CURVAL
..to get the current value, like what mysql_insert_id would return. To populate a primary key, you'd use:
sequence_name.NEXTVAL
To populate a primary in an INSERT statement, you'd use:
INSERT INTO your_table
(pk_id, ..
VALUES
(your_sequence.NEXTVAL, ...)
You can use triggers as an alternative, but they won't return the current value.
What auto_incremented ids? AFAIK, There is no such thing in Oracle. You can simulate the behaviour by adding a trigger on the table and a sequence number but there is certainly no equivalent of mysql_insert_id().
I think you need to go back and find another way to identify your records.
C.

Categories