SCook Posted August 10, 2006 Share Posted August 10, 2006 Hi all,Here's my problem, I'm adding a new item to the databvase, and this item has an image also. The image is named using the primary key of the item. So, what I need is to get the next primary key index number before I upload the image and insert it into the db. I used to knwo how to do this, but I haven't done it in ages, so now I'm up a creek :-) Any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/ Share on other sites More sharing options...
obsidian Posted August 10, 2006 Share Posted August 10, 2006 out of curiosity, why do you need to know the [b]next[/b] key before you insert it? why not insert it and then get the id you just put in with mysql_insert_id()? Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/#findComment-72535 Share on other sites More sharing options...
SCook Posted August 10, 2006 Author Share Posted August 10, 2006 Well, first, I don't use mysql_insert_id. I use mysql_query, and the id is auto_incremented. Now, I could just insert the data, then query the db again with the data to be sure I get the right key, but in the event the db is being used by many users, you could get incorrect data. I guess that's probably the solution, but I know there's a way to do this, I just an't remember, or find the code :-) Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/#findComment-72544 Share on other sites More sharing options...
Orio Posted August 10, 2006 Share Posted August 10, 2006 [code]<?php//dont forget to connect to the db$query="SELECT id FROM `table` ORDER BY id DESC LIMIT 1";$result=mysql_query($query);$row=mysql_fetch_array($result);echo $row['id']; //will echo the last id?>[/code]Just change "id" with the column name and "table" with the table's name.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/#findComment-72564 Share on other sites More sharing options...
sasa Posted August 10, 2006 Share Posted August 10, 2006 lock tableinsert some datamysql_inser_idunlock tableupdate data Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/#findComment-72569 Share on other sites More sharing options...
obsidian Posted August 10, 2006 Share Posted August 10, 2006 [quote author=SCook link=topic=103726.msg413274#msg413274 date=1155224269]Well, first, I don't use mysql_insert_id. I use mysql_query, and the id is auto_incremented. Now, I could just insert the data, then query the db again with the data to be sure I get the right key, but in the event the db is being used by many users, you could get incorrect data. I guess that's probably the solution, but I know there's a way to do this, I just an't remember, or find the code :-)[/quote]ok, check out the manual for mysql_insert_id(). it's not a replacement for mysql_query(), it simply [b]RETURNS THE ID OF THE LAST INSERTED ROW[/b], which apparently is what you're after. so, you would do something like this:[code]<?php$sql = mysql_query("INSERT INTO tableName (myColumns) VALUES (myValues)");if ($sql) { // query successful, echo generated ID echo mysql_insert_id();}?>[/code]this removes the risk of someone inserting another record before you can query for the MAX(id). it also saves you from having to lock your tables (never a good option from a user interface). it returns the last inserted id from your [i]current connection[/i], so you can be sure it's the record you just inserted. Quote Link to comment https://forums.phpfreaks.com/topic/17142-geting-the-highest-primary-key-from-a-mysql-db-with-php/#findComment-72611 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.