Comments on: MySQL: How to get the key of last inerted row in PHP http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: How to fetch the ID of the table to be stored | Yankey Answers http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-193027 Sun, 07 Dec 2014 05:32:43 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-193027

[…] http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get- the-key-of-last-inerted-row-in… […]

]]>
By: Kim http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-21288 Tue, 31 Jan 2012 06:42:29 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-21288

Hello there can anyone help me with my current problem..

Reply  |  Quote
]]>
By: Sebrategy http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-8757 Sun, 13 Apr 2008 13:56:16 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-8757

Thanks a lot for this really straight forward and fool-proof solution!
I’m working on a multi-user Ajax db-system and had exactly this problem.
The fact that
mysql_insert_id($dblink)
gives the last ID from that CONNECTION/SESSION is just perfect!
Thanks again!
Seb

Reply  |  Quote
]]>
By: Luke http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3694 Mon, 02 Apr 2007 20:11:02 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3694

Lokesh, you might need to be more specific with what you want to do there. I’m assuming you have a column “duration” in your table “t” which is in MySQL time format. To get all the values from that column that are greater than 1 you can do:

SELECT * FROM t WHERE HOUR(duration) > 1

Of course you mention duration so perhaps you have 2 columns “start_time” and “end_time”. In that case you can do:

SELECT * FROM t WHERE HOUR(end_time-start_time) > 1

You could use TIME_TO_SEC but I guess HOUR might be more readable.

Reply  |  Quote
]]>
By: Lokesh http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3685 Mon, 02 Apr 2007 10:44:39 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3685

Hello everyone,
As always have a question, Can we write queries inside a procedure[PHP/MYSQL]. Say, in a scenario, I want to find out if the duration is less than an hour and i happen to write a procedure for this, one way(That I know) is to write a block of code to get the duration thats stored in secods in to H:i:s format and then Check for the desired condition, In this can i use TIME_TO_SEC of mysql instead of writing a block of code in my procedure? Any help is highly appreciated

Thanks in advance

Reply  |  Quote
]]>
By: Luke http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3589 Fri, 30 Mar 2007 13:35:16 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3589

Lokesh – this will give you the last inserted id in the current DB session. Note that mysql_insert_id takes a database resource as a parameter. That resource is the same one returned by mysql_connect, and the same one you use to run queries.

You will therefore get the last insert id created with that resource. That resource is unique for each PHP session so it will indeed be the last ID inserted from “your computer”.

Same goes for doing this on the command line. You will get the last inserted ID that you inserted since login.

If you call this function right after logging into the database, without actually running an insert query it will return 0 (zero).

Reply  |  Quote
]]>
By: Lokesh http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3583 Fri, 30 Mar 2007 07:13:47 +0000 http://www.terminally-incoherent.com/blog/2006/12/04/mysql-how-to-get-the-key-of-last-inerted-row-in-php/#comment-3583

Really a good one !
However just wanna know if I get the last inserted id of the row from my computer, because this has to be taken care of when there is a concurrent system/multi user system

Reply  |  Quote
]]>