Sign in to follow this  
ready2learn

[web] Storing player inventory? etc. using PHP & MySQL

Recommended Posts

Hello all, I am developing my first browser-based/text-based online strategy game for fun and to further my programming knowledge. So far things are going smoothly and I can find solutions for most of the obstacles I come across. I am using mostly PHP for functionality (and a bit of JS) with MySQL to store all the data etc. My level of programming for PHP is intermediate and for MySQL fairly basic. JS, CSS and XHTML no probs... also not really relevant but at least you get the picture. One of my problems is that I don't know how to go about storing users inventory items. Would each player have there own MySQL db with an items table and each item added there? That sounds a bit ott... so I guess i'm missing something? I have been reading around a bit and cannot find anything that answers my question directly, maybe I am looking in the wrong places? Hope someone can help Thanks

Share this post


Link to post
Share on other sites
You definitely don't want each user having their own table, let alone their own database.

Here is how I would personally do it.

Users Table: primary_key(user_id)
(user_id) | user_name | ... whatever you need

Items Table: primary_key(item_id)
(item_id) | item_name | ... whatever other information you need for items

User Inventory Table:
user_id | item_id | ... extra data


Essentially, you have one table for the users. Another table for your items. And the inventory table is nothing more than a set of user_ids and item_ids which link items to users, along with additional information such as the inventory position the item is in, or perhaps if the item stacks how many of that item the user has, and so on.

The items table is just a list of all items in your game. An 'instance' of a specific item goes in the inventory table.

You might even want to store, in the inventory table, a unique item identifier which allows you to log where that item has been (who has had it in the past, currently has it, etc) for tracking down any problems there. That could also serve as the primary key for that specific item.

Share this post


Link to post
Share on other sites
Hi,

Thanks for the reply. That helps to clear things up a bit in my head.

This bit sounds very interesting:
Quote:

The items table is just a list of all items in your game. An 'instance' of a specific item goes in the inventory table.

You might even want to store, in the inventory table, a unique item identifier which allows you to log where that item has been (who has had it in the past, currently has it, etc) for tracking down any problems there. That could also serve as the primary key for that specific item.


What if I want the user to be able to store creatures in their inventory, creatures that develop and gain levels exp, health etc. as the game progresses. Would I use the same idea, a table for master creatures and a table for players' creatures stats? If that makes sense.

So player_1 has a dog, it is 2 years old has 5 health and 2 attack.

The dog is stored in a 'Master Creatures Table' along with all the other animals and his base stats are all at 1. The dogs master_creature_id(primary key) is 001.

In the 'Users Creatures' table player_1's dog will have a creature_id(primary key) of 001 and age: 2, health: 5, attack: 2?

Finally, player_1's user_id(primary key) is 001 so in the 'User Inventory Table' I have user_id: 001, master_creature_id: 001, creature_id: 001?

Please forgive me if this is totally wrong. I am new to MySQL and have trouble grasping table relationships etc.

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this