Jump to content

Setting ActionScript variables from PHP


Germaris

Recommended Posts

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):
[code]
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();
[/code]
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:[code]{
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 />";
}
}
}[/code]

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 !!!
Link to comment
https://forums.phpfreaks.com/topic/24652-setting-actionscript-variables-from-php/
Share on other sites

ok, you may have to give us a little more background on [b]how[/b] 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 [b]run[/b] a script with that function called and return the values?
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
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 [b]outside[/b] 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.
Shame on me !
You are perfectly right.

I did what you told me.
Here is the "naked" script:
[code]<?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 />";
}
?>[/code]

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 [url=http://www.germaris.com/~annuaire/test/search_stats.php]http://www.germaris.com/~annuaire/test/search_stats.php[/url]
AT 09:30AM EDT Server was down !!!  Wait a little while and retry. Sorry...
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:
[code]
<?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 )";
?>
[/code]

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!
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 !!!
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.
Here is the code to test.
I have made large cuts to shorten it as much as possible...

[code]<?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;
}

}
?>[/code]

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

And don't worry, we all are learning!  :-)
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:
[code]
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();
[/code]

i may be off here, and then again, this probably has [b]nothing[/b] to do with why it's not working, but i could use a little clarification on that.
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.
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:
[code]
_root.aiFld.text = mySearch.ai;
_root.adFld.text = mySearch.ad;
_root.auFld.text = mySearch.au;
[/code]

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:
[code]
mySearch.ai = _root.aiFld.text;
mySearch.ad = _root.adFld.text;
mySearch.au = _root.auFld.text;
[/code]

i may just be confused because i don't know what's happening elsewhere in your AS
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.
[code]var mySearch = new LoadVars();
_global.mySearch = "";
trace(mySearch);[/code]

In a strict logical point of view, I'll agree with you for
[code]mySearch.ai = _root.aiFld.text;[/code]
instead of
[code]_root.aiFld.text = mySearch.ai;[/code]
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:
[quote author=obsidian link=topic=112233.msg455463#msg455463 date=1161449280]
one other thought: i always thought you had to feed info back into AS as a query string as well (ie, [i]company=xxx&value=21[/i]).
is this not the case?
[/quote]
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
[quote author=Germaris link=topic=112233.msg455514#msg455514 date=1161455948]
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...
[/quote]

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.
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 !!!

:( :( :(
[quote author=Germaris link=topic=112233.msg455583#msg455583 date=1161463521]
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 !!!

:( :( :(
[/quote]

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 [b]in a query string format[/b]? From what I understand, that's the only really successful way to pass multiple variables back from a sendAndLoad() call.
[quote]Is there any way you can think of to return your values to ActionScript from PHP [b]in a query string format[/b]? From what I understand, that's the only really successful way to pass multiple variables back from a sendAndLoad() call.
[/quote]

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?
[quote author=obsidian link=topic=112233.msg455586#msg455586 date=1161463879]
Is there any way you can think of to return your values to ActionScript from PHP [b]in a query string format[/b]?...
[/quote]

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!
Please, don't be sorry.
And, if you want to help me more, there is another little question:

Some variables n returned by the server are equal to 0, i.e. this variables are empty.
In Flash AS, is there a SIMPLE method to prevent empty fields to be populated with the word "undefined" and to have these fields populated with a zero instead?
;D
I don't know of a way with actionscript, but you could run a simple check with PHP when you populate the string, and put in a zero instead of the empty string. If your variable is coming out empty from the database, just do an empty() check as you populate your string:
[code]
<?php
$n = empty($n) ? "0" : $n;
print("&$list=$n");
?>
[/code]

Would that work? Hypothetically, that way, you [b]always[/b] have a value (even if it's zero) returned to actionscript, and then you can deal with the zeros there.
[quote author=Germaris link=topic=112233.msg456329#msg456329 date=1161614099]
It doesn't work...
>:( :o :-\ ???
[/quote]

Can you post an output string as it stands with some empty variables? That would help me see exactly what is getting returned to AS at this point. I'll review your initially posted code in the meantime.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.