Hello,
my problem is related to uploading images (i keep info data in database and images in filesystem).
I plan to generate image path from image ID, that's why i need to get the ID of the last inserted image.

For example: image with ID 123456789 has this path /images/123/456/789

To determine the image path for the new image i want to upload, i need to get the ID of the last uploaded image.

For example, the last uploaded image has the ID: 234565445678.

My question is, how to effectivelly get the last ID from "image" table, without counting all the image records every time a user wants to upload an image.
Is there a good way to get the last ID, maybe to store that ID info in some additional table or something like that.

Member Avatar

diafol

DO NOT use count as you may have non-sequential data, e.g. if an image is deleted. SO the count will not equal the last entry.

In general, to get the last one, you'd add an ORDER BY clause:

SELECT .... ORDER BY image_id DESC LIMIT 1

However, if you need the id immediately after the insert, you can use mysql_insert_id():

http://php.net/manual/en/function.mysql-insert-id.php