Getting Variable from MySQL

Categorized Under: Actionscript No Commented

How to get variable from MySQL with actionscript? Well, first we need to find out how actionscript reads XML file, then we use PHP to get variable from MySQL and translate it into XML. Last, let actionscript do the rest. XML may not only way for this case, but I think it’s the best one.

On this post, I gonna tell you how actionscript deal with XML and how to turn PHP output as XML. Here we go…
By the way, I’m using database on my previous post.

First create PHP file, say list.php, we’re gonna create XML output with this script:

<?php
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_select_db("friend") or die(mysql_error());

$result = mysql_query("SELECT * FROM `user` ORDER BY `user`.`name` ASC");

$xmlstr= "<items>";
while ($row = mysql_fetch_array($result)) {
	$xmlstr.="<item>";
	$xmlstr.="".$row[name]."";
	$xmlstr.="".$row[phone]."";
	$xmlstr.="".$row[email]."";
	$xmlstr.="</item>";
}
$xmlstr.="</items>";

header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
echo $xmlstr;
?>

We just create XML output from PHP. One thing you should know is XML has particular structure, all modify should be refer to XML structure, now next step is preparing your Flash.
On first frame:

stop();

var name:Array = new Array;
var phone:Array = new Array;
var email:Array = new Array;

var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success:Boolean) {

     total = my_xml.firstChild.childNodes;

     for(i=0; i<total.length; i++){
          name.push(total[i].childNodes[0].firstChild);
          phone.push(total[i].childNodes[1].firstChild);
          email.push(total[i].childNodes[2].firstChild);
     }
     play();
};
my_xml.load("list.php");

What happen here is, all data has been stored to each arrays. Next step, we’re gonna load it out into input field text.
On second frame, create input text field, then select Dynamic Text and choose Multiline, type “output_txt” as Instance Name.

stop();

for(i=0; i<total.length; i++){
     output_txt.text += total[i].childNodes[0].firstChild;
     output_txt.text += "\n";
     output_txt.text += total[i].childNodes[1].firstChild;
     output_txt.text += "\n";
     output_txt.text += total[i].childNodes[2].firstChild;
     output_txt.text += "\n\r";
}

Done, it’s easy isn’t it?

Sending Variable To MySQL

Categorized Under: Actionscript 6 Commented

Eventually Flash cannot execute the function on MySQL directly, but through PHP (cmiiw). In order to to send variable to a MySQL database from actionscript, we’re gonna need to understand how PHP and MySQL work. I assume that you have knowledge about MySQL, so we just going deep down on Flash. OK, here we go…

First setup a database and name it “friend”, 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 ;

on actionscript:
lets create a MovieClip, say “form”,

var form:MovieClip = this.createEmptyMovieClip("form", this.getNextHighestDepth());

then, we declare the variable that we want to send by attaching it to previous MovieClip “form”. And also, we want to input that variable dynamically, so we assign it as equal as input text. It means that 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 that variable to PHP by using POST method.

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);
?>

Don’t forget to configure input.php, and it should be work. I think that’s all.