Mariam Dholkawala's Mobile Blog

Flash Lite, Flash Cast, Mobile Widgets, Android, iPhone

My Blog     About     Tutorials

PHP/MySQL with Flash Lite Tutorial

Introduction

I’ve written a short tutorial on Flash Lite interaction with MySQL. Through this tutorial I will try explaining how data can be sent from Flash Lite into a MySQL database using PHP as a middle language. Please note, at the time of releasing this tutorial, I have changed the database password, so the attached code might not function as explained :)


Requirements


Setting up the Database

MySQL databases generally have a basic setup. A database can consist of one or more tables, with each table having fields for storing records.

The process of setting up a MySQL database may vary from host to host. You can use a PHPMyAdmin program for setting up the database (this is what I use), or you can administer your database using PHP scripts. What is important is, after setting up your database, you need to make sure that you have the username, password and the database name handy with you. This information is required for logging into the database records while coding PHP.

I will not go into the specifics of creating database tables or fields and setting up of field parameters because there are ample of tutorials available online that explain that. However, I will talk a little about the database I have set up for our tutorial code. Written below is the structure of a basic database.

Looking at the table below “mariam” is the database name and “GameEntry” is the table name. The table has 3 fields “count_ID” of type INT, “name” of type VARCHAR and “password” of type VARCHAR. For the “count_ID” field, I have set it to a PRIMARY KEY and enabled AUTO_INCREMENT.

mysql> mariam GameEntry;
+---------------+---------------+-------+-------+-----------+-------------------+
| Field       	| Type         	| Null 	| Key 	| Default 	| Extra         |
+---------------+---------------+-------+-------+-----------+-------------------+
| count_ID    	| int(11)      	| NO   	| PRI 	| NULL    	| auto_increment|
| name        	| varchar(15)  	| NO   	|     	| NULL    	|               |
| password    	| varchar(15)  	| NO   	|     	|         	|               |
+---------------+---------------+-------+-------+-----------+-------------------+

Writing the PHP code

With the database now ready, we get into the process of inserting data into the database as well as checking and rejecting duplicate data.

The first thing you must do when you write your PHP code is connect to the MySQL database. This is an extremely important step because, if you are not connected, your commands to the database will fail. For connecting to a MySQL database, also make sure your server has support for MySQL in PHP enabled. If not, please make sure you enable it.
Now getting into the PHP code -



These variables store the database information such as the username and password you set while creating your database and the database name.



These variables values are sent from Flash Lite to be read by PHP, and then finally inserted into the database.



This line tells PHP to connect to the MySQL database server at ' sql.myserver.com' (this is a dummy database server location, you need to write the address of your server) using the username stored in $username and the password in $password.



After connecting to the database server you must then select the database you wish to use. This must be a database to which your username has access. If PHP cannot connect it will stop executing the script and output the text "Could not select database".



We now write a simple query for reading the database. This query will read all the data from the table “GameEntry” and store it in a variable $result.



This piece of code will loop through the entire database result received from the earlier query, and check if the "name" record received from Flash Lite already exists. If a record exists, it will set the $duplicated variable to true.



If a record already exists in a database, PHP will return a value of “Duplicate ID present” to Flash Lite. If a record is not present, PHP will insert the value into the database and return a value of “Entry Successful” to Flash Lite.

If you observe the query above, I am inserting 3 values into the database. The first value is ‘’, because it refers to the “count_ID” field. The records for this field were set to increment automatically so we don't need to send a value. The second and third values refer to the “name” field and the “password” field.



The last line refers a very important command as it closes the connection to the database server. The script will run even if you do not include this command but too many open MySQL connections can cause problems for a web host. It is therefore a good practice to always include this line once you have issued all your commands to the database, to keep the server running well.


Writing the Flash Lite code

Writing the Flash Lite code is extremely simple. We can write a simple 8 line code to send the values to PHP and read back the results.



In the above code, we use the LoadVars class to download data and monitor the data progress. The LoadVars class has an onLoad eventhandler which is invoked when the data is successfully received from PHP.

The LoadVars class can be used alternativly to the loadVariables() function for transferring variables between a Flash Lite application and a server.

The above tutorial is a very simple implementation of reading and writing code to a database. You can enhance this code according to your requirement for data interaction such as submitting high scores in games, allowing only registered members to play game etc.

You can download the complete source code here.

Enjoy Coding!