Jump to content


Photo

Setting ActionScript variables from PHP


  • Please log in to reply
26 replies to this topic

#1 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 12:12 PM

Hi there!

My problem is to get from a table some statistics about how many occurences of some articles exist in the field "list" of this table.
Example:
article ai  ?
article ad  ?
article au  ?

In my Flash .fla File I wrote this function (mySearch is an already declared global function):
displayStats = function () {
	_root.errorMsg.text = "GO";
	mySearch.unlock = "1";
	mySearch.onLoad = function(ok) {
		if (ok) {
			_root.aiFld.text = mySearch.ai;
			_root.adFld.text = mySearch.ad;
			_root.auFld.text = mySearch.au;	
                 _root.errorMsg.text = "OK";
		}
		else {
			_root.errorMsg.text = "BAD";;
		}
	};
	mySearch.sendAndLoad("search_stats.php", mySearch, 'POST');
};
_root.displayStats();
All the dynamic fields for receiving the variables exist and are well formatted.

In my PHP Script, with all the necessary stuff for good connection with the DB, I wrote this query:
{
	if
	($unlock < 1) print "NO ACCESS";
	else {
		GLOBAL $db,$table;
		$query =mysql_query ("SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 0 )");// or die ( "select error " .mysql_error () );
		$res = mysql_query($query) or die (mysql_error());
			while (list($list, $n) = mysql_fetch_row($res)) {
    			print "$list : $n <br />";
			}
	}
}

The query is working when tested in the SQL Tab of phpMyAdmin and returns these results:

list     n
_____________
ai       72
ad       325
au       127

When I test in real world (i.e. from a button contained in the Flash .swf File), it shows that communication exists (error message: "OK") and that the query is working but my dynamic fields do not receive the values of the variables. Instead, they all are populated with the mention "undefined".

So... what's wrong here?

I thank in advance anybody who can help me !!!

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 12:30 PM

ok, you may have to give us a little more background on how you're running your flash connection to the PHP script. is the PHP function simply included on the page where the Flash is imbedded, or are you making an http request through your flash to run a script with that function called and return the values?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 12:53 PM

Thanks for your reply!

I am making an http request through my flash to run a general script with that function called and returning the values.
I can give you more details on the writing of that script if needed (but it's quite a large one...)

A+
Gerard

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 12:58 PM

what happens when you hit your PHP page directly? i see what you're doing with your AS now, and you should be fine with your sendAndLoad() call. i guess my question is this: how is your PHP script running? i see the opening and closing brackets around your whole thing, but is that a function or what? syntactically, the code you posted above won't work. try setting up a basic PHP form, and make sure that your search_stats.php does exactly what you need it to do outside of flash, and then we can work on your connection.

you may have already done all of this, but it helps for me to narrow down the problem to where it is actually dying.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 01:14 PM

Shame on me !
You are perfectly right.

I did what you told me.
Here is the "naked" script:
<?php
include '2006_key.php';
mysql_connect( $host ,$user ,$pass) or die(mysql_error () ); 
mysql_select_db( $db ) or die(mysql_error () );
$table = '_annuaire';
$query =mysql_query ("SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 0 )");
$res = mysql_query($query) or die (mysql_error());
while (list($list, $n) = mysql_fetch_row($res)) {
	print "$list : $n <br />";
}
?>

Script 2006_key.php is present and running well for all my scripts.

And I got this message:
"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1"

Which is like hebrew to me...

PS: Here is the URL http://www.germaris....earch_stats.php
AT 09:30AM EDT Server was down !!!  Wait a little while and retry. Sorry...

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 01:36 PM

aha! check this out: you're running mysql_query() on the result of a mysql_query() function ;)
instead of calling mysql_query() on the first line, just set your query string, and then you should be good:
<?php
// change this line: 
$query =mysql_query ("SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 0 )");
// to this:
$query = "SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 0 )";
?>

notice on your next line, you're trying to call mysql_query() a second time? well, in mysql errors, for future reference, a "Result ID" is a response you have received from a mysql query.

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 02:29 PM

Well, this is the situation:

1 - Standalone script works well (please test it with the URL I previously mentioned). Your remarks were perfect!!! Congratulations!

2 - Pasting the code in the general script:
The script works as the error message indicates this in my Flash file but dynamics fields do not receive any value for the variables (they still are populated with "undefined").
:-(

May I send you the whole script for diagnosis?

PS: This is beginning to drive me crazy !!!

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 03:07 PM

which code are you wanting the diagnosis on? i'm assuming based on your last post that it's the AS that's not acting right, correct? if so, i'll be glad to look over it, but when it comes to that side of things, i'm still learning, too, so i may not be much help. rather than posting my email here, send me a PM, and i'll give you my addy.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 03:22 PM

Here is the code to test.
I have made large cuts to shorten it as much as possible...

<?php
include '2006_key.php';
mysql_connect( $host ,$user ,$pass) or die( mysql_error () ); 
mysql_select_db( $db ) or die( mysql_error () );
$table = '_annuaire';
//------
//Here almost 18 functions take place
//------
function stats($unlock)
{
	if
	($unlock < 1) print "ACCESS DENIED";
	else {
		GLOBAL $db,$table;
		$query = "SELECT list, count( * ) AS n FROM $table GROUP BY list HAVING ( n > 0 )";
		$res = mysql_query($query) or die (mysql_error());
		while (list($list, $n) = mysql_fetch_row($res)) {
    		print "$list $n <br />";
		}
	}
}
//------
//To decide which "case" to choose
//------
if(isset($HTTP_POST_VARS["action"]))
{
	switch($HTTP_POST_VARS["action"])
	{
		//------
		//Here almost 18 "case" take place
		//------
		case "stats":
		$result = stats($HTTP_POST_VARS['unlock']);
		print $result;
		break;
	}
	
}
?>

Please report on the Flash AS I wrote on my first posting.

And don't worry, we all are learning!  :-)

#10 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 03:49 PM

on question... why are you declaring your function as you are? i thought that you only used that syntax when you were applying the defined function to a trigger? shouldn't it simply be:
function displayStats() {
	_root.errorMsg.text = "GO";
	mySearch.unlock = "1";
	mySearch.onLoad = function(ok) {
		if (ok) {
			_root.aiFld.text = mySearch.ai;
			_root.adFld.text = mySearch.ad;
			_root.auFld.text = mySearch.au;	
                 _root.errorMsg.text = "OK";
		}
		else {
			_root.errorMsg.text = "BAD";;
		}
	};
	mySearch.sendAndLoad("search_stats.php", mySearch, 'POST');
}
_root.displayStats();

i may be off here, and then again, this probably has nothing to do with why it's not working, but i could use a little clarification on that.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#11 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 04:25 PM

You are right but it doesn't matter... Any of both method is OK...
I use this method everywhere in my Flash file...
I use the function on entering the frame where the page is located.
For sure this has nothing to do with the problem we are working on.

#12 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 04:45 PM

ok, i'm running through a few sendAndLoad() examples myself, and i always have a separate LoadVars() object to receive the result. i don't suppose it's required, but it might be a good practice ;) . where are you instantiating mySearch as a LoadVars() object?

also, this code confuses me a little bit as well:
_root.aiFld.text = mySearch.ai;
_root.adFld.text = mySearch.ad;
_root.auFld.text = mySearch.au;

are you actually loading your text fields with the value of your search object, or are you trying to actually load you LoadVars object with the values of those fields? in my mind, those should be reversed:
mySearch.ai = _root.aiFld.text;
mySearch.ad = _root.adFld.text;
mySearch.au = _root.auFld.text;

i may just be confused because i don't know what's happening elsewhere in your AS
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#13 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 04:48 PM

one other thought: i always thought you had to feed info back into AS as a query string as well (ie, company=xxx&value=21).

is this not the case?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#14 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 06:39 PM

mySearch is LoadVars declared (instantiated) in the very first frame of the movie and in a dedicated separate layer which covers the whole timeline so it is always available from any frame.
var mySearch = new LoadVars();
_global.mySearch = "";
trace(mySearch);

In a strict logical point of view, I'll agree with you for
mySearch.ai = _root.aiFld.text;
instead of
_root.aiFld.text = mySearch.ai;
But:
1 - the manual describe it as I wrote it
2 - I use this writing in my whole movie in which as many as forty search can be performed and anything is running fine! Some of them are far more complicated and they run in a breeze.

As I am an open-minded person and as I want to be successful, I'll give a try to your suggestion even if I am not convinced...

For this remark:

one other thought: i always thought you had to feed info back into AS as a query string as well (ie, company=xxx&value=21).
is this not the case?

I understand what you mean...
I think the problems sits in the PHP Script.
Is the data returned well formatted to be understood by the Flash file ?
That's the point and that's the question...


A+
Gerard

#15 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 08:27 PM

I understand what you mean...
I think the problems sits in the PHP Script.
Is the data returned well formatted to be understood by the Flash file ?
That's the point and that's the question...


have you tried return the variables to actionscript in the method i proposed? if your data is not the type that can be parsed into a query string (which search results sound like they may not work that way very well), you may need to consider having your PHP script actually generate an XML file of results and spit either success or failure out to actionscript. that way, once your script returns a value, if it's success, your actionscript can then load the formatted results from the XML file.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#16 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 08:44 PM

Ouch, my friend!
This sounds very complicated for me regarding my skills and far from beyond them!
I'm afraid not to be able to achieve something I don't understand at all...
I'm 65 and even if I'm learning something new everyday, this is a too big chunk for me !!!

:( :( :(

#17 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 08:45 PM

What do you think we can do now?

#18 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 08:51 PM

Ouch, my friend!
This sounds very complicated for me regarding my skills and far from beyond them!
I'm afraid not to be able to achieve something I don't understand at all...
I'm 65 and even if I'm learning something new everyday, this is a too big chunk for me !!!

:( :( :(


Hey, don't let age be an excuse ;) ... you've obviously learned quite a bit in the last couple years if you're using ActionScript like you are. it hasn't been around all that long, yet you're doing quite a bit with it! I mean it with all respect, but if you have been able to get to the point you are, I guarantee you would be able to learn what I suggested. Now, I may have been a little over zealous with the recommendation. Let's back up a step:

Is there any way you can think of to return your values to ActionScript from PHP in a query string format? From what I understand, that's the only really successful way to pass multiple variables back from a sendAndLoad() call.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#19 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 21 October 2006 - 09:04 PM

Is there any way you can think of to return your values to ActionScript from PHP in a query string format? From what I understand, that's the only really successful way to pass multiple variables back from a sendAndLoad() call.


When I mentioned my age, it was tongue in cheek...
:-)

I already used $flashstring to send the data to the Flash file (in the general PHP Script of which I sent to you a piece).
But when I used this string it was for returning multiple lines of data each of one separated with a line break and ALL THE LINES WERE TO BE LOADED INTO A SINGLE DYNAMIC FIELD !!!
NOT INTO MULTIPLE DIFFERENT FIELDS as it is in our today problem...

It sounds to be quite different, don't you think?

#20 Germaris

Germaris
  • Members
  • PipPipPip
  • Advanced Member
  • 97 posts
  • LocationCanada

Posted 23 October 2006 - 01:27 PM

Is there any way you can think of to return your values to ActionScript from PHP in a query string format?...


You were right!
The problem is solved.

String send by the PHP Script to the Flash file must be (IN THE ORIGINAL SCRIPT I POSTED):

print "&$list=$n";

Many thanks for your efforts, your valuable help and the time you dedicated to my problem!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users