
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?

