0

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


?>
5
  • Does dataOnLoad ever get called? Commented Oct 22, 2014 at 13:35
  • @ ethrbunny yes i called it in function dataOnLoad(evt:Event).... or maybe you explain a little bit. tq Commented Oct 22, 2014 at 13:43
  • You declared it - I'm wondering if the POST callback ever occurs. Commented Oct 22, 2014 at 13:48
  • So may you pliz show me a way referring on my above flash code? am not outstanding programmer though.@ ethrbunny Commented Oct 22, 2014 at 13:53
  • I tried to change the line highscores.htmlText = returnVars.score; to highscores.text = returnVars; but it displays all content of html settings found in php together with data. Is there a way to fix it so as to display only data in flash? Commented Oct 22, 2014 at 18:08

1 Answer 1

0

As I mentioned in my previous answer, a textfield in Flash won't play well with HTML tables.

In your PHP file, you need to replace everything between the first and last echo with the following:

echo "scores=<b>Best Times:</b><br />";

$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 "$i2. <b>$name</b> | <b>$school</b> | $score | $date <br />";

    $i2++;
    $i++;
}

Note that I have added back the scores= variable. This shouldn't cause any issues, as long as you're using the following line in AS3:

highscores.htmlText = returnVars.scores;

Separating your variables into name/value pairs means that you can pass other variables from PHP if you need to.

Update:

Having taken a look at your files, the whole thing runs perfectly; it successfully submits to the database and loads data back. However:

I think the issue you're having is with Flash caching your scores.php file.

This is fairly common when doing a URLRequest() on a file you've previously loaded. The most straightforward way to deal with this is to change this line:

fileLoader.load(new URLRequest("http://127.0.0.1/Y/scores.php"));

To this:

fileLoader.load(new URLRequest("http://127.0.0.1/Y/scores.php?rand=" + Math.random() * 999999));

All that does is add a random number to the end of your load as the 'garbage' variable rand. Flash sees this as you loading a different file, and so makes a fresh request rather than pulling it from the cache.

Other than that, everything in your files seems to run exactly as it should.

Sign up to request clarification or add additional context in comments.

10 Comments

I tried this it displays nice in browser but again it gives same error in flash Can't parse loaded file: Error #1069: Property score not found on String and there is no default value. btw what is the variable score for? @indextwo
@mchucha Sorry, my mistake - you should be pulling returnVars.scores - plural, not singular. As I mentioned, having the value in a variable means you can add other variables to your PHP output. If you prefer, you can probably take that out and just dump returnVars into your textfield. If that works, just remember to remove scores= from your echo.
I tried to make plural but it doesn't but after i take out .scores and remove also from echo it displays this in text field "connection okayBest Times:" but no data is retrieved then @indextwo
@mchucha Just to confirm: is your textfield set to multiline in Flash? By default they're not. It sounds like it's loading your information fine, but not showing anything after Best times: because that's followed by a new line.
Yes i set it to dynamic text field and multiline @indextwo. Btw is there a way i can send you my files so that you can have a close look to them e.g E-mail Tq?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.