Im trying to figure out a way to access a row I 'just created' via the primary key.

As it being a primary key it is auto incremented.

I am using php/mysql to create the row then I require to access that same row (via the primary key) to be able to do the required adjustments.

So any ideas?

Thanks, Regards X

10 Years
Discussion Span
Last Post by TopDogger

Entries in a database are not supposed to be identifiable by their row position. You identify it by a field in the row.

Please show us your code to generate the tables and the insertion.


Theres 500 lines worth, so I wont be spamming the forums. In short I just need some sort of concept or idea that allows you to access a row not based on the Primary Key. Eg:


Now what could you use instead of that pk?

I was thinking just all the newly added cells but on the off chance that ALL cells are the same on a previous row there would be trouble...

Hence is there anyway to access the primary key before it has been made (the next one in the list - due to auto incremement) or any other way to identify the row?



One of the main reason to use primary key is, because of its uniqueness. Say for example you have 4 columns in your table(name, age, city, country). If you don't have a primary key/unique id to differentiate between rows, your query may not do what you expect it to. Eg. Select * from user where name='name1' AND age='30'; might return several rows!
If you have a table with an auto_increment column (primary key/unique id) you can fetch the data (you want) using the unique id.

If you are inserting a record to a table with auto_increment field.
insert into table (id,name,age) values ('','name3','40'); You can access the last generated auto increment field (last inserted record's id) by using last_insert_id() or mysql_insert_id() .

If I am off topic, my apologies. (I didn't understand your question!)


nav33n's solution will work if you just want to capture the ID for the last row that you wrote to the table.

If you wish to access a different row, you really need to know how to use the SELECT statement to filter the data.

Here is a link to a freebie MySQL cheatsheet that may help.

Basically, if you want to access a unique row, you need to SELECT one or more columns of data that represent a unique value or combination of values--but you need to know those values before you can pull the related data from a table.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.