www.flasharcadia.com.">
First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Nick Kuh | Website: http://www.flasharcadia.com

Setting up the first section of the PHP script

Write 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
$newPlayer"; $scores = "$scores
$newScore"; } printf("&players=%s", $players); printf("&scores=%s", $scores); mysql_close($link); }

Let's breakdown the code

this 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
$newPlayer"; $scores = "$scores
$newScore"; }
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.
» Level Intermediate

Added: 2002-04-21
Rating: 6 Votes: 22
(10 being the highest)
» Author
Nick is a Flash Developer for London based web design agency Edition Interactive. His main role is online games design and development for clients such as Channel Four Television.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.