Retrieve last updated timestamp in MySQL - php

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

Related

Will msql_insert_id() give me an incorrect result if another autoIncrement happens before I call it?

I'm trying to add a row to a table, and then immediately get its unique ID, which is simply an autoincremented column.
Googling tells me PHP can do this through the mysql_insert_id() method, which
Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
Is the "previous query" limited in scope to this specific connection, or is it possible that if somehow another row is inserted before I call it, it will return the wrong ID?
Are there any better suggestions on how to get or set a unique ID for a row? I'd make the unique ID on the client-side, but there are multiple clients, so that's not really possible.
Thanks.
Yes, it's limited to connection, so it will return a reliable value.
Try to use mysqli-functions or PDO, MySQL will be deceprated.

Can I get columns of last inserted row?

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.

What would be the mysql equivalent of lastInsertId() for Updates to get the last Updated Id of the record

So I am using PDO statements and I have a very handy command
$db->lastInsertId();
which is helping me track the last inserted record's id. I checked but did not find clear answers if there is a handy function like that. It would be cubersome to run a whole query to get that, as it is being mentioned in some pages.
How to get the last updated record?
How to get exactly the id of the last inserted record in mysql?
Thanks.
I think mysql_insert_id() is what you are looking for.
You can find more info on the function here http://php.net/manual/en/function.mysql-insert-id.php

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()

Categories