Can any one give me SQL query to get id (Which is primary key and auto_incremented) of last row inserted
I know there are many answers on this forum about this but no one consider about concurrency problems (or may be i just missed them) that if in mean time any other person execute insert query before getting me last id(from the code below) so this creates problem.
Here is the code that i think is not worked well in my situation
$result = mysql_query("
SHOW TABLE STATUS LIKE 'Media'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
but here also a code may solve my problem
<?php
$id = mysql_insert_id();
?>
I just want to know if this code gets the insert id of row executed from actual user or the row id of user who inserted after the actual users row execution(or the last row of table).
I know this may be a silly question but i am a PHP developer doesn't know about MYSQL
Any help is appreciated.
mysql_insert_id will return the ID of the last row inserted in the transaction. If you create a new connection to MySQL for each page and don't use multiple threads within a page, mysql_insert_id should do what you want even in the presence of multiple INSERTs happening on different connections, provided you're not doing anything tricky within your PHP page.
Related
Does PDO::lastInsertId() returns last inserted id among all the tables in database? or from a specific table currently it working on?
I have a doubt like if multiple users working on website and they try to execute some same kind of a operation and which effects to the same tables in the database. Then if it happens at the same time will PDO lastInsertId handle that? Will POD lastInsertId return lastInsertedId for relative users. I mean like will it return correct row id to the correct person who inserted that row? or will it mixed up? like the id of row I inserted will return to someone else, and I will get a row id which is not from the row I inserted.
I need to update and/or insert in a Postgres DB, a String value with letters and numbers, which must be incremented of +1 (through php strings functions) everytime there is an UPDATE.
I need to LOCK this table, in order for the php to complete its flow in inserting or updating it avoiding others that open the page to receive the same result of the SELECT.
The second arrived, would wait for the first to finish.
The second arrived, will ALWAYS make an UPDATE.
This update could generate a FILENAME0023, if there were 22 updates after the first insertion.
There could be more connections at the same time, I need to reserve the first result of the SELECT for the first one who connected to this php page.
The flow would be:
LOCK
SELECT column FROM table WHERE column = 'FILENAME0001';
IF NOT EXIST { INSERT INTO table (column) VALUES ($my_column); }
ELSE { UPDATE table SET column = '$my_new_column' WHERE column = '$my_column'; }
UNLOCK
The variable $my_new_column is a SUBSTR php function that would cut the number part of the string and will then be incremented of +1.
This link is helping me a lot: THIS.
But it does not contain everything.
I also tried working with stored procedure: LINK
But I should work on the php when it is an update because I cannot increment a DB value like shown HERE, because I do not have a INT but a string and I can not change this
Anyone who can help me?
I'd like to share my code, but believe me I'd rather start fresh, all the codes I tried are leading to nowhere.
you should try to use
`INSERT ON ON DUPLICATE KEY UPDATE`
Here link for more example about it
Apparently this...
$lastid = $wpdb->insert_id
...will give me the last inserted row (as noted here: How to get last inserted row ID from wordpress database?).
But how can I target a specific table, and get information from the latest inserted row after a form was just submitted?
For example, I have a table called 'license' and each row contains the columns 'email' and 'name' (among others).
The idea is that after I insert something into that row with a form, I need to display the email and name on screen.
Any help would be awesome.
You can use the value of an auto-increment column.
You might already have the ID column doing so.
In a new SELECT-statment use ORDER BY myCol DESC the auto-increment-coloumn and use LIMIT 1 to only get the one with the highest value (wich is the most recent one)
According to what you already have you have to use the last inserted id for a new query.
$query = "select * from license where id = ".$lastid;
Using the result of this query you can print out the other information of the last inserted row.
If you need more info on the php code, provide your code so we can edit it accordingly
ID of the Last Inserted Record in general (in your example from Wordpress), can only fetch (as the name says) only the ID (AUTO_INCREMENT) from a column as a result of the most recently executed INSERT statement. mysql doc
There is no direct way if specifying "get me lastID of XYZ table" for a particular in reverse point in time, only works as described above.
Even with the proper usage in php you need to be careful if other INSERT statements on other tables (ones you don't want) are executed before you try to fetch LAST_INSERT_ID, so you don't end up getting IDs from unwanted tables.
Your possible workarounds:
do a SELECT from your desired table ordered by ID desc, e.g:
SELECT id FROM xyz ORDER BY id DESC LIMIT 1;
this is presuming id is an AUTO_INCREMENT or similar
another option is if possible in code locate the "position" of INSERT statements of the table you want e.g. xyz and fetch the id right there with:
$wpdb->insert_id
I am building a web application, which can be used by multiple users simultaneously. They can add data at the same time.
I have a table named doctor_main as follows
Screenshot of DB http://img687.imageshack.us/img687/7033/testxqz.png
Once a record about a doctor is added, I want the id of the inserted record(which is an auto increment field) to be returned.
I know i can use methods like LAST_INSERT_ID() and mysql_insert_id. But i don't know how it behaves.
I need the exact id of the record which is inserted by that particular user.
Sometimes, if two users are trying to insert a record, the id which is returned shouldn't get exchanged.
To achieve this what kind of mysql function should i use ?
There's a whole page in the manual dedicated to this subject:
How to Get the Unique ID for the Last Inserted Row
Here's a quote from that page that should help answer your question:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
mysql_insert_id() returns exactly the id of the last inserted record, so if you just echo mysql_insert_id() you'll get the id of the very last inserted row.
According to the docs, the mysql_insert_id will return to you the exact id of the insert that you done before.
LAST_INSERT_ID() operates per-connection; multiple users will use multiple connections, so there will never be an exchange of IDs between users.
LAST_INSERT_ID() and mysql_insert_id works fine. Each client will receive the last inserted ID for the last statement that client executed.
If you are suspicious on the mechanism of last_insert_id, then you may assign the id by hand not by auto_increment feature of MySQL.
I am using adodb for PHP library.
For fetching the id at which the record is inserted I use this function "$db->Insert_ID()"
I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ?
The reason I am asking this is because I use this last insert id for further processing of other records and making subsequent entries in the related tables.
Is this approach safe enough or am I missing something.
Please help me formulate a proper working plan for this so that I can use the last insert id for further inserts into the other table safely without having to mess up with the existing data.
Thanks
Yes, it's safe for concurent use. That's because LAST_INSERT_ID() is per-connection, as explained here:
The ID that was generated is
maintained in the server on a
per-connection basis. This means that
the value returned by the function to
a given client is the first
AUTO_INCREMENT value generated for
most recent statement affecting an
AUTO_INCREMENT column by that client.
This value cannot be affected by other
clients, even if they generate
AUTO_INCREMENT values of their own.
This behavior ensures that each client
can retrieve its own ID without
concern for the activity of other
clients, and without the need for
locks or transactions.
The $db->Insert_ID() will return you last insert id only so if you are inserting many records and want to get id of each last inserted row, then this will work successfully.
I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ?
It will return only the most recently inserted id.
In order to get ids for multiple inserts, you will have to call INSERT_ID() after each statement is executed. IE:
INSERT INTO TABLE ....
INSERT_ID()
INSERT INTO TABLE ....
INSERT_ID()
...to get the id value for each insert. If you ran:
INSERT INTO TABLE ....
INSERT INTO TABLE ....
INSERT_ID()
...will only return the id for the last insert statement.
Is this approach safe enough or am I missing something.
It's safer than using SELECT MAX(id) FROM TABLE, which risks returning a value inserted by someone else among other things relating to isolation levels.