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 :)
Related
I have this two tables:
I also have a dynamic form in which it contains table and the user can add rows and the data from it will be inserted in tblcamealsformdetails but the basis for inserting it is the id from tblcamealsform. How do I insert all the values to the two tables simultaneously?
Here's my form:
You will enter data first in table tblcamealsform. You insert ID from that query.
That ID you will use then to insert the rest of the data, along with the insert ID, in table tblcamealsformdetails.
So you don't do it simultaniously. You add the dependencies first.
To get the insert-id from the last query you executed, you will need mysql_insert_id().
See http://php.net/manual/en/function.mysql-insert-id.php
In answer to the comment what will happen if multiple users use the form at the same time:
Since you open a mysql connection at the top of your script which will result a unique connection pointer and all of the mysql-functions you call automatically reference to that pointer I think mysql_insert_id() will always reference to the latest query performed by the current connection. So another thread by another user would not interfere with this.
Please note that I am not 100% sure about this though.
Anyway: I am using this for about 10 years now some of which include high-traffic websites and I have never experienced any problems using this method, so in my opinion you can safely use it.
There is one exception to this:
You must always call mysql_insert_id() immediately after executing the query you want the ID for. If you execute any other query in the meantime (for example, you call a method of another object which performs an insert-query) mysql_insert_id() will return the ID of that query instead.
This is mistake I have made in the past and which you have to be aware of.
I'd like to point you using LAST_INSERT_ID:
when doing multiple-row inserts, LAST_INSERT_ID() will return the value of the first row inserted (not the last).
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.
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.
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.
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.