Errant_Shadow Posted November 16, 2009 Share Posted November 16, 2009 I've been having a hard time finding this because I'm not sure how to ask it succinctly. Basically, I'm entering new data rows into a database, but I then need to retrieve the auto-incremented primary key for use immediately after. I can't know what the key will be before I create the row (that I'm aware of), so I was wondering if there was anything I could do to retrieve that value without creating and running a separate query. Link to comment https://forums.phpfreaks.com/topic/181674-solved-fetch-auto-incremented-index-id-while-the-row-is-being-created/ Share on other sites More sharing options...
corbin Posted November 16, 2009 Share Posted November 16, 2009 You could probably get by with mysql_insert_id. Depending on what you're doing, you might should read the "Caution" in the Notes section. Link to comment https://forums.phpfreaks.com/topic/181674-solved-fetch-auto-incremented-index-id-while-the-row-is-being-created/#findComment-958210 Share on other sites More sharing options...
Errant_Shadow Posted November 16, 2009 Author Share Posted November 16, 2009 Ah, that's awesome. No, none of my tables are BIGINT sized so there shouldn't be a problem. Thank you very much Link to comment https://forums.phpfreaks.com/topic/181674-solved-fetch-auto-incremented-index-id-while-the-row-is-being-created/#findComment-958214 Share on other sites More sharing options...
Errant_Shadow Posted November 20, 2009 Author Share Posted November 20, 2009 as a note: mysql_insert_id() MUST be called immediately after the insert query, even if the only other query run was not an insert. It will return 0 otherwise, rather than the insert id. Link to comment https://forums.phpfreaks.com/topic/181674-solved-fetch-auto-incremented-index-id-while-the-row-is-being-created/#findComment-961774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.