|
|
Search Tutorials
Setting up the first section of the PHP scriptWrite the php script using your favourite text editor. Make sure you save it as highscores.php The php script is split into two parts: the first part loads the current set of high scores and the second adds the current player's score to the database and then refreshes the high scores in the flash movie. Here's the first section:
if ($command=="init"){
$link = @mysql_connect("localhost", "YOURUSERNAME",
"YOURUSERPASSWORD");
if (!$link){
print "&result=Error connecting to database";
exit;
}
if (!@mysql_select_db("YOURDATABASENAME")){
print "&result=Couldn't select database";
exit;
}
$players = "";
$scores = "";
$innerquery = "SELECT * FROM highscores ORDER BY scores DESC";
$innerresult = @mysql_query($innerquery);
$numPlayers=mysql_num_rows($innerresult);
for($loop=0;$loop<$numPlayers; $loop++) {
$newPlayer = mysql_result($innerresult,$loop,"players");
$newScore = mysql_result($innerresult,$loop,"scores");
$players = "$players
Let's breakdown the codethis is the tag used to begin a php script if ($command=="init"){
The script looks for a variable called command. If it exists and it's value equals init then
the script executes the first section. So by setting our flash action script variable
command to equal init before we called highscores.php for the first time this section of
the php script will be executed.
$link = @mysql_connect("localhost", "YOURUSERNAME",
"YOURUSERPASSWORD");
The new link variable connects to our database using your username and password.
if (!$link){
print "&players=Error connecting to database";
exit;
}
if (!@mysql_select_db("YOURDATABASENAME")){
print "&players =Couldn't select database";
exit;
}
If the php script is unable to connect to the database then it quits the rest of the script and
creates sends the sting information back to the flash movie. For anyone who's imported
variables into flash from a text file this format will look very familiar. Flash will interpret
the information sent as the value of a variable called players.If all of the connections to our mySQL data are successful then the main body of our code will be executed: $players = ""; $scores = "";This ensures that these two variables are reset so that our results are new. $innerquery = "SELECT * FROM highscores ORDER BY scores DESC"; $innerresult = @mysql_query($innerquery);The first line creates a php variable which holds a basic SQL command. It selects al l (* - this symbol means all in a SQL command) of the data from our highscores table, it then arranges it in score order (lowest score first) and then reverses the order so that the player with the best score is now in first place. The second line executes the SQL command. $numPlayers=mysql_num_rows($innerresult);This variable calculates the number of rows in our highscores table using a standard mySQL command. for($loop=0;$loop<$numPlayers; $loop++) {
$newPlayer = mysql_result($innerresult,$loop,"players");
$newScore = mysql_result($innerresult,$loop,"scores");
$players = "$players
Ok, so here we're creating a loop using almost identical syntax to action scripting. The
loop will finish when it has got results from the total number of rows in our mySQL table.
$newPlayer and $newScore are temporary variables used to store the player's name and
score from the current row being looped. The values of $newPlayer and $newScore get
added to $players and $scores respectively each time the script loops. Notice how in php
you can place a variable in the middle of a string. If the dollar sign wasn't used then php
would not recognise our variables.
printf("&players=%s", $players);
printf("&scores=%s", $scores);
Finally we create two Flash variables called players and scores which will be sent back
to our flash game. In the layout above, php will replace the %s with whatever appears
after the comma.
mysql_close($link); }It's always good manors to close the connection to your database when you've finished receiving data.
|
||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||
|