
Sending Variable To MySQL
Eventually Flash cannot execute any MySQL functions directly but through PHP (cmiiw). – here’s a funny thing about PHP, at the beginning it’s stand for Personal Home Page but it is now said to stand for PHP: Hypertext Preprocessor after improvement of its capability. In order to sending variable to a MySQL database, we’re gonna need to understand how variable being sent from Flash using ActionScript to PHP, then later being proceed in PHP and finally stored into MySQL (that’s it).
Here’s an illustration of how the variable being sent to MySQL

If you don’t have MySQL on your system or you don’t have any idea what MySQL is, please read set up a server. And for you who already have can go on to next step …
First, open PHPMyAdmin on your web server then setup a database and name it “friend” (we’re gonna make a phone book database), and fill it with this query:
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`phone` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=72 ;
This will create a table called ‘user’ which contains properties (id, name, phone and email)
on ActionScript:
Create a MovieClip, set “form” as instance name,
var form:MovieClip = this.createEmptyMovieClip("form", this.getNextHighestDepth());
then, what can we do is, set name of variable that we want to send by attaching it to previous MovieClip “form”. Suppose you want to set ‘x’ as a name of variable, just put ‘x’ after the MovieClip – for this case would be: form.x And in case you need to send more than one variable, just set it in different names.
Next step, set the value.
Without giving the value to the variable, it would be set as ‘undefined’ by default, so make sure you set all variable’s value. And also, since we want to determine the variable’s value later when the web is run, we could assign it as equal as input text. So, whatever we type on input text, would be the variable’s value.
form.name = name_txt.text;
form.email = email_txt.text;
form.phone = phone_txt.text;
next, we’re going to send those variables to PHP by using POST method. This is the process what we talking about, the value that you given on input text will transferred to PHP.
form.loadVariables("input.php","POST");
form.onData = function(success:Boolean) {
for (var a in this) {
trace([a, this[a]]);
}
}
on input.php
<?php
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_select_db("friend") or die(mysql_error());
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$sql_user = "INSERT INTO `friend`.`user` (`name`,`phone`,`email`)
VALUES ('$name', '$phone', '$email');";
$result_user = mysql_query($sql_user);
?>
In the process of storing database we need to credential access, so don’t forget to configure input.php. As you can see on input.php, there are 3 variables (name, email & phone) which is taken from previous process using POST method. Those 3 variables will be stored in database ‘friend’ on table ‘user’ on each field. To retrieving those variable to Flash click here!

