Unqiue ID columns - PHP or MySQL query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Unqiue ID columns - PHP or MySQL query?

Post by mjseaden »

Hi,

I have several tables - e.g. Products, Manufacturers, that has a unique ID associated with each row - e.g. ProductID, ManufacturerID.

When I delete rows in the middle of the table, I want to reassign the IDs into numerical order.

Also, when I insert a row at the end, I want to know the last ID in the table without having to read the whole table.

How do I carry out both operations, and should this be carried out via a MySQL query, or via PHP?

Many thanks
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

dont reassign the IDs, if you have used them as foreign keys then you would need to update all those records also. Dont do it.

To get the last generate ID use

mysql_insert_id() funtion

http://www.php.net/manual/en/function.m ... ert-id.php
Post Reply