I am new to database, i faced a very strange problem recently:
I create a database as a leaderboard server for my game where it holds the player names and scores. I created a php scipt to query the database, and the game will open the php url with GET arguments.
My submit score function works perfectly. I use phpadmin to check. Everytime I submit it will update the database correctly.
The problem is retrieving the leaderboard, my game will retrieve the leaderboard every 10 secs. It succeeds everytime. But the scores it gets are always not the lastest records in database, all are previous records. And after some hours, it can get newer data.
Below are my troubleshoots: 1 I didn't include "commit" in my submit score php script. So I manully do commit, but game still can't get the lastest data.
php script:
$query = "SELECT * FROM `space_scores` ORDER by `score` DESC";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; ($i < $lines)&&($i<$num_results); $i++)
{
$row = mysql_fetch_array($result);
echo $i+1 . "," . $row['name'] . "," . $row['score'] . "|";
}
You need to turn off query caching
set session query_cache_type=0;
See this link for details:
http://dev.mysql.com/doc/refman/5.5/en/query-cache.html
Your php server is caching the sql query to avoid repetetive querying and reduce load
Turn it off by
set session query_cache_type=0;
in the start of the php script for select.
Also, running a db query every 10 seconds is not a good idea. When you scale up to 1000 users (suppose) you will have at least 100 requests per second which is not very good for your server.
Consider running the php script which querys the db and writes the results to a xml or comma delimited file every 1 minute or so by setting up a cron job and get this file from the server every 10 seconds instead of the php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With