Hello,

I've looked on google for information about storing multiple pieces of information in one database column, but I can't find anything helpful. If I want to have some sort of etc. inventory, and there is more than one 'item', how would I store multiple items inside the database?

Thanks

Recommended Answers

All 7 Replies

Im not really sure what your asking. Could you give an example of what you want stored.

Thanks

Table Inventory:

user_id | weapon_id | armor_id | etc_items
-------- -+---------------+------------+------------
............ | ................. | .............. | this is the important part

How would you store multiple things or items in that spot for just one user?

Table Inventory:

user_id | weapon_id | armor_id | etc_items
-------- -+---------------+------------+------------
............ | ................. | .............. | this is the important part

How would you store multiple things or items in that spot for just one user?

No need to do it that way, you can save all the etc_items for one particular user some other table which refers your primary table's key; even though you can save all the items for single user in just one column with comma seperated values.

But what if you don't know how many items there are going to be? Is there a way to set a variable amount of columns?

No you can't have a variable number of columns.

The way I would go about it is to create a new table for your etc_items column, and just have it have 2 or more columns as needed,
one being user_id, which should be a foreign key to the user_id field in the user table, and then a item_id field.

That way, if a user has a variable number of items, you can just add/delete records in the new table.

But what if you don't know how many items there are going to be? Is there a way to set a variable amount of columns?

yes, that's the way i mean like above post, when i say a seperate table.You have to insert as many entries for particular record in the master table.
Then while fetching those contents you can do something like -

"select * from secondary_table where weapon id = $w_id"; 
//where $w_id it refers to the primary table.

Network 18 is correct, this is the best way to go around your problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.