views:

89

answers:

3

I ‘concat’ an id of just loaded image into mysql user record like this.

 Userid … Photos_ids
 12         1 2 5 7

And this photo_id should be unique. (To create unique file name). Now I get a current last id from memcache. But it's not really safely, as far as I understand.

It there any other ideas? (I do not add any other records into db, so PRIMARY KEY can be used only if I'd add some else special table for this... It's not a good idea).

+1  A: 

Have two tables, make the id column the Primary Key and Auto Increment

Example:

CREATE TABLE users(
    id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name CHAR(60) NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE pictures (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     user_id MEDIUMINT NOT NULL
     PRIMARY KEY (id)
 );

In the example, INSERTing a new pictures would automatically increase the id by 1.

Gordon
I said - I have no photos table... I've got only users table and every users record contain list of ids (simple number of photo). It's a bad idea to create new table just to count photos :)
MInner
@MInner what makes you think it is a bad idea? Also, please clearify if you want the photo_id columns to really contain the ids of the photos or just the total number of images for this user, e.g. is photo_id really supposed to be a counter?
Gordon
It really contains photos numbers. Well, you forced me to think about it more careful. Hm. May be it's not as slow as I used to think about it.
MInner
A: 

Have a separate table for images.

id (auto-increment)
image_url
..any other info you need about images
user_id (foreign key)

user id obviously shows which user is the owner of that image. It points to the id column of users table. Adjust according to your db model :)
Sejanus
It's the simplest way to do that. But it's much slower to select them all, order and etc. I simply cache some kind of result of all there select and etc. in photo column. It's much faster. (For example to show all users photos).
MInner
SELECT `image` FROM `images_table` WHERE `user_id` = '$user_id' - is it really that slow? Or maybe I got your question/situation wrong, sorry in that case :)
Sejanus
May be it's not as slow as I used to think about it.
MInner
+2  A: 

Why not use PHP's uniqid function?

Sinan