Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Supporting Technologies > Server-Side Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-25-2004, 08:31 PM   #1
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default How to send PHP data to Flash?

OK....here's what I'm trying to do. I want to basically click a button and it populates on dynamic text field showing all the fiels in my one table called players (it's for a hockey league). So I have a database called 'stats' a table in there called 'players' and a bunch of fields (i.e. name, goals etc)...I wrote a .php page with the following code
PHP Code:
<?php

   
//Set up constants
   
define ('MYSQL_HOST''localhost');
   
define ('MYSQL_USER''myName');
   
define ('MYSQL_PASS''myPassword');
   
define ('MYSQL_DB''stats');
   
//If we fail to connect
   
if (! mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) )
   {
      die(
'Failed to connect to host "' MYSQL_HOST '".');
   }
   else
   {
      echo 
'Connected to mysql server ' MYSQL_HOST ' as user ' MYSQL_USER ' ';
   }
   
//Tell mysql which database to use
   
mysql_select_db(MYSQL_DB);
   echo 
'Database ' MYSQL_DB ' selected for use.';
   
   
//Select entire table
   
$result mysql_query('SELECT * FROM players'); 
   
   echo 
$result;

?>
** As a side note when I view this page in a browser I get 'Connected to mysql server localhost as user myName
Database stats selected for use.'' So that part looks cool.....

Now for flash....here's where I get stuck....I have a new flash file open with 2 layers. On the first layer I have my dnamic text box and on the second an actions layer. I give the dynamic text box a variable and instance name of 'content' and on the actions layer I assign the following:
PHP Code:
loadVariables("http://localhost/script.php""this""GET"); 
I test in flash and nothing.....I know I'm missing stuff....can any1 help?

Last edited by CyanBlue; 04-26-2004 at 08:54 AM.
rubbersoul is offline   Reply With Quote
Old 04-25-2004, 09:45 PM   #2
killGerald.com
Olympic Dad
 
killGerald.com's Avatar
 
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
Default

A couple things that might help

1) "content" is reserved by flash (it's used in the Loader class [for the Loader component]). You shouldn't use reserved keywords for var names - try $content or _content instead. And try content_txt for the textField.
2) you didn't mention if you assigned the 'content' variable to the results of the php page.
3) to read data from an external file, flash looks for &'s to tell it where to look -
you should try (within the script.php) echo "&result=".$result;
4) I'm not sure if you used the onLoad(success) function, try this if you haven't

Code:
//declare the LoadVars object:
results_lv = new LoadVars();
//inside the onLoad function we'll use with result_lv (see below), "this" refers to the
//LoadVars object, not the main timeline.  Use the following code to make a
// reference to the main timeline
results_lv._Parent = this;  //make sure you spell _Parent as is - _parent is reserved

reults_lv.onLoad = function(ok){
   if(ok){
          this._Parent.content_txt.$content = this.result;
   }else{
          this._Parent.content_txt.$content = "Unable to load from db";
   }
};

//it's a good idea to have the load function after the onLoad - just in case the 
//data comes in super-fast:
results_lv.load("http://localhost/script.php", "GET");

//give some feedback to the user as the data loads:
if(!results_lv.loaded){
  content_txt.text = "data loading";
}


5) I haven't tested any of this - so it may not work as expected .

-Gerald.
killGerald.com is offline   Reply With Quote
Old 04-25-2004, 10:04 PM   #3
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default

OK...I tried your code.....I get the 'data loading' part....yipeee!!!! At least I'm a bit closer. As for your question #2
Quote:
2) you didn't mention if you assigned the 'content' variable to the results of the php page.
I don't understand. I gave you all of my PHP above. What you see is what you get. I have nothing else....for the text box, should content_txt be used as both the instance and variable name? Still need help....thxs
rubbersoul is offline   Reply With Quote
Old 04-25-2004, 11:41 PM   #4
killGerald.com
Olympic Dad
 
killGerald.com's Avatar
 
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
Default

Hey rubbersoul,

nevermind question number two.

Give this a try, it should get your code working:

(within the onLoad function):

Code:
  if(ok){
          this._Parent.content_txt.text = this.result;
   }else{
          this._Parent.content_txt.text = "Unable to load from db";
   }
Lemme know if it works out for you,

Gerald.
killGerald.com is offline   Reply With Quote
Old 04-26-2004, 06:47 AM   #5
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default

Nope....not working. Let me show you what I used
PHP Code:
//declare the LoadVars object:
results_lv = new LoadVars();
//inside the onLoad function we'll use with result_lv (see below), "this" refers to the
//LoadVars object, not the main timeline.  Use the following code to make a
// reference to the main timeline
results_lv._Parent this;  //make sure you spell _Parent as is - _parent is reserved

reults_lv.onLoad = function(ok){
if(
ok){
          
this._Parent.content_txt.text this.result;
   }else{
          
this._Parent.content_txt.text "Unable to load from db";
   }
};

//it's a good idea to have the load function after the onLoad - just in case the 
//data comes in super-fast:
results_lv.load("http://localhost:300/script.php""GET");

//give some feedback to the user as the data loads:
if(!results_lv.loaded){
  
content_txt.text "data loading";

Is this right???? Still not working!

Last edited by CyanBlue; 10-11-2004 at 03:43 PM. Reason: PHP tag is applied
rubbersoul is offline   Reply With Quote
Old 04-26-2004, 07:06 AM   #6
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Howdy and Welcome...

You are missing the fact that Flash understands the external data in the form of variableName=value pair combined with '&' sign...

So, your first two echo lines, Connected to mysql... and Database..., will work in your web browser, but it is useless and making Flash unable to understand what it needs to do... So, comment out those two lines...

This line, echo $result;, needs to be looped through the array and create the variableName1=value1&variableName2=value2& format...

Lastly, please use PHP tag or CODE tag to format the script... and post your question to the right forum... I am moving this thread to the Server-side Scripting forum because it looks like your question is more toward the PHP side rather than the Flash side...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 04-26-2004, 08:34 AM   #7
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default

I thank you for your input....however, it's like your speaking Chinnese to me Can you look at my code that I've provided and show me (the code) of what I can use to correct the problem. Thanks.
rubbersoul is offline   Reply With Quote
Old 04-26-2004, 07:56 AM   #8
killGerald.com
Olympic Dad
 
killGerald.com's Avatar
 
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
Default

it looks good to me, but why don't you upload the fla so i can take a gander at it
(oh, and did you modify the php file with" echo "&result=".$result "?)
-gerald
killGerald.com is offline   Reply With Quote
Old 04-26-2004, 08:36 AM   #9
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default

Yes. I did modify the PHP file. I'm at work right now so I can't do anything about uploading thee .FLA, but I will this evening. Thanks for all your help!
rubbersoul is offline   Reply With Quote
Old 04-26-2004, 06:20 PM   #10
rubbersoul
Registered User
 
Join Date: Apr 2004
Posts: 28
Default

OK...I've included my sample flash .fla and included here once again my .php code. Hope someone can help.....

My PHP PAGE CALLED STAT.PHP
PHP Code:
<?php

//Set up constants
define ('MYSQL_HOST''localhost');
define ('MYSQL_USER''root');
define ('MYSQL_PASS''********');
define ('MYSQL_DB''stats');
//If we fail to connect
if (! mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) )
{
    die(
'Failed to connect to host "' MYSQL_HOST '".');
    }
    else
    {
        echo 
'Connected to mysql server ' MYSQL_HOST ' as user '
            
MYSQL_USER '<br>';
            }
//Tell mysql which database to use
mysql_select_db(MYSQL_DB);
echo 
'Database '  MYSQL_DB .  ' selected for use.';

//Select entire table
$result mysql_query('SELECT * FROM players'); 

echo 
"&result=".$result;

?>
and now just take a look at my .fla and you can hopefully make some sence outta my mess.
Attached Files
File Type: zip Untitled-2.zip (2.3 KB, 119 views)
rubbersoul is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:15 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.