am creating my flash project that accepts 4 variables i.e. name, school, score and date. However the connection between FLASH, PHP and MYSQL works fine and i can send data to MySQL database via flash with no problem. Now my problem is that i can't retrieve and view the data back to flash. I created a textfield named highscores for retrieving data in flash. And am sure my php code for retrieving is fine since i can view data in browser. So is there a way to retrieve my data in flash? here is my coding for flash:
str.text = "";
myschool.text = "";
myscore.text = "";
//Here i declared a textfield named highscores
//var text:String = highscores.text;
btn_submit.addEventListener(MouseEvent.CLICK, submitted);
function submitted(e:MouseEvent)
{
if(!str.length) {
status_txt.text = "Please enter your name";
}
else if (!myschool.length) {
status_txt.text = "Please enter your school name";
}
else if (!myscore.length) {
status_txt.text = "Please enter your score";
}
else {
var myrequest:URLRequest = new URLRequest("http://127.0.0.1/Y/sendscore.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.name = str.text;
variables.school = myschool.text;
variables.score = myscore.text;
myrequest.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myrequest);
}
}
function dataOnLoad(evt:Event)
{
trace("Data submission complete");
var returnVars = evt.target.data;
trace("***********************");
for (var myVars in returnVars) {
trace(myVars + ": " + returnVars[myVars]);
}
trace("***********************");
MC_success.alpha=100;
//status is a custom flag passed from back-end
}
btn_scores.addEventListener(MouseEvent.CLICK, loadScores);
function loadScores(e:MouseEvent):void {
var fileLoader:URLLoader = new URLLoader();
fileLoader.addEventListener(Event.COMPLETE, scoresLoadComplete);
fileLoader.load(new URLRequest("http://127.0.0.1/Y/scores.php"));
}
function scoresLoadComplete(evt:Event):void {
try {
var returnVars = evt.target.data;
highscores.htmlText = returnVars;
trace("Data retrieved successfully");
for (var myVars in returnVars) {
trace(myVars + ": " + returnVars[myVars]);
}
trace("***********************");
//highscores.htmlText = returnVars.scores;
} catch (err:Error) {
trace("Can't parse loaded file: " + err.message);
}
}
HERE IS MY PHP CODE FOR RETRIEVING DATA
<?php
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Create INSERT query
$qry = "SELECT * FROM highscores ORDER BY score DESC LIMIT 5";
$result = @mysql_query($qry);
$num=mysql_numrows($result);
if($num > 10)
{$num = 10;}
//echo "writing=Ok";
echo "<b><center>Best Times:</center></b><br /><table>";
//echo "scores=<b><center>Best Times:</center></b><br /><table>";
$i=0;
$i2=1;
while ($i < $num) {
$name=mysql_result($result,$i,"user");
$school=mysql_result($result,$i,"school");
$score=mysql_result($result,$i,"score");
$date=mysql_result($result,$i,"date");
echo "<tr><td align=left valign=top>$i2.</td><td align=center valign=top><b>$name</b> <b> | $school</b> | $score | $date</td></tr><tr><td colspan=2><hr></td></tr>";
$i2++;
$i++;
}
echo "</table>";
//$urlRefresh = "scores.php";
//header("Refresh: 15; URL=\"" . $urlRefresh . "\"");
exit();
mysql_close();
?>
dataOnLoadever get called?