I am creating RPG type game. I pull out player’s information in every page loaded from two tables with JOIN (it is information about member: age, name, his all game’s stats and etc) and sometimes this information is required for me, sometimes not at all. Two tables consists of ~60-70 rows. Here is an example:
$query = mysql_query("SELECT m.*, i.*
FROM members AS m
INNER JOIN information AS i
ON n.m_id = s.m_id AND n.secret_code= '$code' AND n.password = '$password'")
or die(mysql_error());
I run this query every page. And I put this query on header.php file which is included in index.php file. I am doing everything through index.php file with including files, like that:
If it is page index.php?id=gym I include folder gym with all its files.
What would you suggest me? Re-write all system and insert into each file a query which would pull out only necessary information? Use Sessions? But if I buy, for example, two items, how can I know when to get data on how much member has money? Thank you very much.
Some information could be stored in the session. Non-volatile things like user name, player/character name, etc… That’s not going to change very often. But things like current score, gold/credits available, etc.. which change frequently and should be kept consistent would most likely have to be pulled from the database each time. Consider what happens if they open two browser windows and do some shopping in each. Both load up saying you’ve got 500 gold available and both could potentially purchase something for 499 gold at the same time.
But, it comes down to what’s faster, really. Do some benchmarking and see if pulling 60-70 bits of data from the database for each hit is faster/slow than pulling a mix of database/session data.