A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Variables from PHP to AS3

  1. #1
    Member
    Join Date
    Jan 2006
    Posts
    75

    resolved [RESOLVED] Variables from PHP to AS3

    Hi all,

    i'm echoing variable in a PHP script which i get from a mySQL database to an AS3 flash file but i can only ever get undefined as the output when i try to trace any of the variables in flash. Here is my code...

    PHP Code:
    <?php
    // get data from mysql database
    include("admin/config.php");
    $con mysql_connect($dbHost$dbUser$dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " mysql_error());
    mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " mysql_error());
    $theQuery mysql_query("SELECT * FROM flashArtists");
    // result to be sent to flash AS3
    $result "";
    for (
    $i 0$i mysql_num_rows($theQuery); $i++)
    {
        if (
    $i != 0)
        {
            
    $result .= "&";
        }
        
    $thisRow mysql_fetch_row($theQuery);
        
    $result .= "name" $i "=" urlencode($thisRow[1]);
        
    $result .= "&songs" $i "=" urlencode($thisRow[2]);
    }
    echo 
    $result;
    ?>
    the output of this when viewed in a browser looks like this...

    Code:
    name0=Jay+Z&songs0=20&name1=Madonna&songs1=35&name2=Oasis&songs2=51&name3=Eminem&songs3=72&name4=Michael+Jackson&songs4=65&name5=Metallica&songs5=96&name6=Tool&songs6=21&name7=Rage+Against+The+Machine&songs7=26&name8=Rolf+Harris&songs8=3&name9=Timmy+Mallet&songs9=5&name10=Boyzone&songs10=18&name11=Blue&songs11=6&name12=Rihanna&songs12=56&name13=Meatloaf&songs13=13&name14=Rachel+Black&songs14=2&name15=Kylie+Minogue&songs15=31&name16=Apparat&songs16=2&name17=Pet+Shop+Boys&songs17=17
    This is my flash AS3 code

    Code:
    package 
    {
        import flash.display.Sprite;
        import flash.events.*;
        import flash.net.*;
    
        public class main extends Sprite
        {
            public function main()
            {
                var request:URLRequest = new URLRequest("bannerArtists.php");
                var getVariables:URLLoader = new URLLoader();
                getVariables.dataFormat = URLLoaderDataFormat.VARIABLES;
                getVariables.addEventListener(Event.COMPLETE, completeHandler);
                getVariables.load(request);
    			
                try
                {
                    getVariables.load(request);
                } 
                catch (error:Error)
                {
                    trace("Unable to load URL: " + error);
                }
            }
    		
            private function completeHandler(event:Event):void
            {
    		var urlVariables:URLVariables = new URLVariables();
    		urlVariables.decode(event.target.data);
    		trace (urlVariables.name0); // shows "undefined"
    		trace (urlVariables.songs3); // shows "undefined"
            }
        }
    }
    I've tried so many different ways to get this to work but nothing is helping.

    I should say that i'm running this on a local server using WAMP until i get the code working and i'll then be uploading it to my webspace. Is it possible that my local server isn't set up right? Are there any settings which this code relies on which i should check in PHP, or Apache?

    Can i get flash to cycle through all of the data it has in "urlVariables" to see what is in there? It might help with debugging my code.

    Any help would be greatly appreciated.

    Cheers

    LL

  2. #2
    Member
    Join Date
    Jan 2006
    Posts
    75
    p.s.

    if i trace "event.target.data" in the "completeHandler" function i get this...

    Code:
    %3C%3Fphp%0D%0A%2F%2F%20get%20data%20from%20mysql%20database%0D%0Ainclude%28%22admin%2Fconfig%2Ephp%22%29%3B%0D%0A%24con%20=%20mysql%5Fconnect%28%24dbHost%2C%20%24dbUser%2C%20%24dbPass%29%20or%20trigger%5Ferror%28%22Failed%20to%20connect%20to%20MySQL%20Server%2E%20Error%3A%20%22%20%2E%20mysql%5Ferror%28%29%29%3B%0D%0Amysql%5Fselect%5Fdb%28%24dbDatabase%29%20or%20trigger%5Ferror%28%22Failed%20to%20connect%20to%20database%20%7B%24dbDatabase%7D%2E%20Error%3A%20%22%20%2E%20mysql%5Ferror%28%29%29%3B%0D%0A%24theQuery%20%3D%20mysql%5Fquery%28%22SELECT%20%2A%20FROM%20flashArtists%22%29%3B%0D%0A%2F%2F%20result%20to%20be%20sent%20to%20flash%20AS3%0D%0A%24result%20%3D%20%22%22%3B%0D%0Afor%20%28%24i%20%3D%200%3B%20%24i%20%3C%20mysql%5Fnum%5Frows%28%24theQuery%29%3B%20%24i%20%20%29%0D%0A%7B%0D%0A%09if%20%28%24i%20%21%3D%200%29%0D%0A%09%7B%0D%0A%09%09%24result%20%2E%3D%20%22&%22%3B%0D%0A%09%7D%0D%0A%09%24thisRow%20=%20mysql%5Ffetch%5Frow%28%24theQuery%29%3B%0D%0A%09%24result%20%2E%3D%20%22name%22%20%2E%20%24i%20%2E%20%22%3D%22%20%2E%20urlencode%28%24thisRow%5B1%5D%29%3B%0D%0A%09%24result%20%2E%3D%20%22&songs%22%20%2E%20%24i%20%2E%20%22=%22%20%2E%20urlencode%28%24thisRow%5B2%5D%29%3B%0D%0A%7D%0D%0Aecho%20%24result%3B%0D%0A%3F%3E
    ... which is just an exact, encoded copy of the php file i posted previously.

  3. #3
    Member
    Join Date
    Jan 2006
    Posts
    75
    I don't know why, but i just changed the start of the php $result variable from
    $result = "";
    to
    $result = "foo=bar";
    and all of a sudden it's working.

    I have no idea why that happened but if anyone could explain it to me so that i understand it and so that other people with the same problem understand it i'd appreciate it.

    Cheers

    LL

Tags for this Thread

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