Can I get columns of last inserted row? - php

Hi I'm using Codeigniter and would love a solution in CI but can suffice with mysqli or PDO.
I was wondering if there is a way to get a column of the last inserted row in MySql 5.1.54
The reason is I'm inserting a null value which then a trigger changes that null into some calculated value and instead of getting the last inserted id and querying for that value.
Is there some way this can be done?
In case I wasn't clear : I know I can run a query on last inserted id, I mean can I get last inserted "column"?

You can get the last inserted id by
$this->db->insert_id();
and select the last row and use it normally
ref Query Helper

I Guess the answer #Eric is looking for is NO.

The PDO comes with lastInsertId method. You can then SELECT * FROM table WHERE id=return_of_this_function.

Related

Retrieve last updated timestamp in MySQL

I have the following query:
UPDATE myTable SET myTime=utc_timestamp() WHERE myID=something
I would like to retrieve the timestamp that was set, preferably within the same query of with something like mysql_insert_id().
Thank you all !
First, mysql_insert_id() is doing a new query. Why you want the data returned you currently saved? Then you might be able to access via the Datacontainer/Variable you got it from.
mysql_insert_id() is just returning the last inserted id of a table. Its not delivering any more data.
To really answer your question, you should tell what you want to do first.
IMPORTANT: mysql_insert_id() wont return you the id of an updated row !
In case you want to get the last inserted id of an updated row - take a look at this answer https://stackoverflow.com/a/14002784/3048505

get mysql last insert id, after inserting record using PDO

I use PDO prepared statements to insert records. And uses lastInsertId to get id of inserted record. But is this good?
Because what happens when several users insert at same time? lastInsertId will conflict in that situation? So I should use transaction?
please help
Every PHP script basically lives on its own. When several users insert at the same time, user A will get his last insert's row idA and user B will get her last insert's row idB.
It will return the correct ID, don't worry about it.

How to get exactly the id of the last inserted record in mysql?

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.

How do I know when a field/row in mysql updated successfully?

Right now, I have my database setup so that I insert a new row. But if the row I'm trying to insert is a duplicate (based off of my primary key id), then I increment the count field by 1.
Right now I want to know when I was able to create a new row instead of having it increment count. How would I do this in an efficient/proper manner?
The current method I'm thinking of doing this, is by querying the id first and checking if it exists. But I feel like there's a faster/better way. I've also read about triggers but I've heard that they're bad/risky to use.
Use INSERT ... ON DUPLICATE KEY UPDATE...
Then query for affected_rows (as #Tarek Fadel suggested). MySQL will return 1 if the row was inserted, or 2 if existing row were updated.
Use your database AUTO INCREMENT option for your primary ID field. Only propper solution.
Here you have mysql reference, but that exist in just every database engine:
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
How about an auto_increment on the id column?
Otherwise you might use SELECT MAX(id) FROM TABLE to retreive the highest id and add one to it, but that isn't thread-safe since another user might execute the same insert at the same time. Auto_increment fixes that for you.
PHP's mysql_affected_rows()

Retrieving the index of an inserted row

I'm trying to keep the database tables for a project I'm working on nice and normalized, but I've run into a problem. I'm trying to figure out how I can insert a row into a table and then find out what the value of the auto_incremented id column was set to so that I can insert additional data into another table. I know there are functions such as mysql_insert_id which "get the ID generated from the previous INSERT operation". However, if I'm not mistaken mysql_insert_id just returns the ID of the very last operation. So, if the site has enough traffic this wouldn't necessarily return the ID of the query you want since another query could have been run between when you inserted the row and look for the ID. Is this understanding of mysql_insert_id correct? Any suggestions on how to do this are greatly appreciated. Thanks.
LAST_INSERT_ID() has session scope.
It will return the identity value inserted in the current session.
If you don't insert any rows between INSERT and LAST_INSERT_ID, then it will work all right.
Note though that for multiple value inserts, it will return the identity of the first row inserted, not the last one:
INSERT
INTO mytable (identity_column)
VALUES (NULL)
SELECT LAST_INSERT_ID()
--
1
INSERT
INTO mytable (identity_column)
VALUES (NULL), (NULL)
/* This inserts rows 2 and 3 */
SELECT LAST_INSERT_ID()
--
2
/* But this returns 2, not 3 */
You could:
A. Assume that won't be a problem and use mysql_insert_id
or
B. Include a timestamp in the row and retrieve the last inserted ID before inserting into another table.
The general solution to this is to do one of two things:
Create a procedural query that does the insert and then retrieves the last inserted id (using, ie. LAST_INSERT_ID()) and returns it as output from the query.
Do the insert, do another insert where the id value is something like (select myid from table where somecolumnval='val')
2b. Or make the select explicit and standalone, and then do the other inserts using that value.
The disadvantage to the first is that you have to write a proc for each of these cases. The disadvantage to the second is that some db engines don't accept that, and it clutters your code, and can be slow if you have to do a where on multiple columns.
This assumes that there may be inserts between your calls that you have no control over. If you have explicit control, one of the other solutions above is probably better.

Categories