A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Getting PHP queries properly displayed in AS3

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    107

    Getting PHP queries properly displayed in AS3

    Hello there,

    I'm coping with a problem which drives me mad. For days, I am searching for a solution to display XML data in Flash which has been sent from PHP. To understand why I all want this, let me explain.

    I have a game in Flash where people can post their highscores when the game is over. The highscore will be sent from AS3 to the MySQL database, where PHP grabs it with a query. This part is working succesfully. But, I want to sent the values back to Flash to show the best highscores. I tried to generate an XML in my PHP and call it from my ActionScript. I get this:

    PHP Code:
    <data><player><id>51</id><user></user><score></score></player><player><id>52</id><user></user><score></score></player><player><id>53</id><user></user><score></score></player><player><id>57</id><user></user><score></score></player><player><id>58</id><user></user><score></score></player><player><id>34</id><user>sdfd</user><score>0</score></player><player><id>38</id><user>987d</user><score>0</score></player><player><id>39</id><user>Niet</user><score>0</score></player><player><id>50</id><user>test!</user><score>0</score></player><player><id>59</id><user>Dit is een te</user><score>0</score></player></data></highscores
    Those are my database values, wrapped up in an xml tree in PHP. But I just need the values to be displayed into Flash. How do I do this??

    PHP Code:
    public function loadHighScores():void 
            
    {
                    var 
    myrequest:URLRequest = new URLRequest("http://stap.iam.hva.nl/~schildm001/Game/getHighScoreList.php");
                    
    myrequest.method URLRequestMethod.POST;
                    var 
    variables:URLVariables = new URLVariables();
                    
    variables.test "Hello";
                    
    trace(variables);
                
                
                    
    myrequest.data variables;
                    var 
    loadPHP:URLLoader = new URLLoader();
                    
    loadPHP.dataFormat URLLoaderDataFormat.VARIABLES;
                    
    loadPHP.addEventListener(Event.COMPLETEdataOnLoad);
                    
    loadPHP.load(myrequest);
                
                
                    
                    
            
                    
            }
            
            private function 
    dataOnLoad(e:Event):void 
            
    {
                
            
                
                var 
    xmlData:XML  XML(e.target.data);
                
    trace(xmlData.player[0]);
                
    trace(xmlData.player.id[0]);
                
    trace(unescape(xmlData[0]));
                
            
            
            } 

    PHP Code:
    <?php


    //Include database connection details
    require_once('connection.php');
    //Connect to mysql server
    $link mysql_connect(DB_SERVERDB_USERDB_PASS);
    if(!
    $link) {
    die(
    'Failed to connect to server: ' mysql_error());
    }

    //Select database
    $db mysql_select_db(DB_NAME);
    if(!
    $db) {
    die(
    "Unable to select database");
    }





    $list mysql_query("SELECT id, user, score FROM highscores ORDER BY score ASC LIMIT 10 "$connection);

                    if(!
    $list)
                    {
                        die (
    "db query failed" mysql_error());    
                    }
                    
                    
                    
    $results '<data>';
                    
                    while (
    $row mysql_fetch_array($list))
                    {   
                        
    $results .= "<player>";
                        
    $results .= "<id>".$row['id']."</id>";
                        
    $results .= "<user>".$row['user']."</user>";
                        
    $results .= "<score>".$row['score']."</score>";
                        
    $results .= "</player>";
                    }
                    
    $results .= '</data>';
                    print(
    $results);  
        
                    while(
    $row mysql_fetch_assoc($list))
                     {
                        echo 
    "<name>" $row["user"] . "</name>\n";
                    }    
                    
                    echo 
    "</highscores>\n";




    ?>
    <!--    while ($row = mysql_fetch_array($list))
                    {
                        echo  $row['user'] . $row['score'];
                        
                        
                    } -->
    <!--$test = "This is a test"; 

    $test2 = "This is a test2"; 

    echo $test;

    echo $test2;
     -->

    Thanks in advance!

    Mushrambo

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You're missing an opening <highscores> tag.
    Also, since there are no <player> nodes in the root you'll need to use E4X notation.

    trace(myXML..player[0])

    You might look into http://www.silexlabs.org/amfphp/

  3. #3
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    Thanks for the answer.

    Still not working somehow. I don't exaclty know why. I think i'll dive into AMFPHP then.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center