Highscore Example


Run Sample Java App | View PHP Source

This is a very simple example of how to set up a highscore table in a game. Using PHP you can interact with a MySQL database to store and view the scores.

Settings

Take a look at the script source for the PHP script that interacts with the MySQL database. There are a few settings you have to adjust before you can use the script. After you set up the database on your webserver, change the settings so the PHP script knows where to go to store data. Here's an explanation of the settings: The rest of the $GLOBALS are not settings (so don't touch those).

Connecting to the Script

Because all data passed to the script via GETVARS, we can query the script just by using a standard URL address. Your Java application only needs to use the URL class to point to the script. Then just use the openStream() to get an InputStream object to read the data from the script after the command has been executed. For example, to receive the list of players and their scores currently in the database:

URL url = new URL("http://yoursite.com/highscore.php?action=list&access_code=1234");
InputStream in = url.openStream();
BufferedReader b = new BufferedReader(new InputStreamReader(in));
// now you can use b.readLine() to read the data from the script until it returns null

Commands

There are only 3 commands you can pass to the script: install, submit, and list. You specify what command you want by passing action=cmd to the URL of the script.