View Full Version : php remoting
Mak Valley
October 7th, 2006, 07:45 AM
i installed the AMFPHP Project from adobes website and i'm kinda stuck right now... i can define a variable in the actions panel and trace it as well as loops and stuff, but i can't execute any mysql. i have i feeling i'm seriously missing something, i just don't know what.
here's some code:
$dbconnect=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("awards", $dbconnect);
$selectaward=mysql_query("SELECT * FROM teacher");
while ($r = mysql_fetch_array($selectaward)){
trace($r['award']);
}
knowledge on the subject will be seriously helpful, greatly appreciated, and i'll e-mail you a pizza.
ghjr
October 7th, 2006, 09:05 AM
Setup your remote function as it is setup on this page (http://amfphp.sourceforge.net/docs/sendrecordsets.html), so your actually returning the query as a response to flash. Then from flash call the remote function and whatever comes back you will treat as a RecordSet, like:
var myRecordSet:RecordSet = RecordSet(myResult.result);
More on RecordSets in the Flash IDE help, or http://livedocs.macromedia.com/flashremoting/mx/Using_Flash_Remoting_MX/UseASData6.htm
Cheers,
ghjr
Mak Valley
October 7th, 2006, 09:42 AM
i'm not sure if this step is unecessary for me or not.. but this is from the amfphp site:
"It is because you have not properly included the Remoting classes in the movie. You can do so by clicking Window > Other Panels > Common Libraries > Remoting, drag and drop both RemotingClasses and RemotingDebugClasses on the stage, then delete them. This should place a copy of the files in your library. You can double-check that they are included in the library (Ctrl+L)."
i have flash 8 on osx.... it doesn't go Window > Other Panels > Common Libraries > Remoting........ it just goes Window > Common Libraries... with NO Remoting...
is this unecessary for me? or is there some other way?
ghjr
October 7th, 2006, 10:01 AM
Did you install the remoting components?
http://www.macromedia.com/software/flashremoting/downloads/components/
Cheers,
ghjr
Mak Valley
October 7th, 2006, 07:27 PM
yes, i installed those as i was instructed to.... but i have no "remoting" selection like i described in my previous post.. it appears i must drag "RemotingClasses and RemotingDebugClasses on the stage"... which i can't find..
AndyM103
October 8th, 2006, 03:51 AM
Try the actionscript.com tutorial on this - there's a nice connector class that handles everything!
http://www.actionscript.com/Article/tabid/54/ArticleID/AMFPHP/Default.aspx
Mak Valley
October 9th, 2006, 12:11 AM
i got everything working now.. i can run the "hello world" test and stuff, i guess now my problem is understanding the concept of this. is there a tutorial somewhere or could somebody knowledgable break down the process for me because im having trouble coding this, on the flash and php side....thank you
sanneke
October 9th, 2006, 12:17 AM
this is not an answer to your question, but i can't find out how to post, can you help, and yes i feel stupid,
Mak Valley
October 9th, 2006, 07:27 PM
let's say i have the following code working properly via html:
$dbconnect=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("awards", $dbconnect);
$selectaward=mysql_query("SELECT * FROM teacher");
while ($r = mysql_fetch_array($selectaward)){
echo $r['award'];
}
what changes would i make to that to be able to call it remotely through flash?
edit: and for the sake of me not understanding anything at all about remoting lets pretend this is the only snippet of code i want to transfer..... i read up about remoting everywhere i could and they all talk about everything having to have the same name (class function). but nobody explains what it does or why.. could somebody drop some knowledge on me?
scotty
October 10th, 2006, 03:19 AM
Moved to Server-Side :cool:
scotty(-:
ghjr
October 10th, 2006, 04:04 PM
You actually return the query to Flash:
function returnResultset() {
$dbconnect=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("awards", $dbconnect);
return mysql_query("SELECT * FROM teacher");
}
And then you could deal with the query results from inside flash, instead of in PHP... that's the beauty of remoting. :)
So, in flash you would handle your response something like this:
function handleResult(re:ResultEvent):Void {
var myRecordSet:RecordSet = RecordSet(re.result);
}
Now with that new recordset you can do a bunch of cool things, as explained here: http://livedocs.macromedia.com/flashremoting/mx/Using_Flash_Remoting_MX/UseASData6.htm or in the Flash help on RecordSets.
Cheers,
ghjr
Mak Valley
October 10th, 2006, 06:24 PM
thanks ghjr, it's starting to make a lot more sense to me...... but i still dont get have the as function handleResult corresponds to the php function returnResultset.... could you shed some light on this for me?
Mak Valley
October 10th, 2006, 08:48 PM
AS
import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
NetDebug.initialize();
var _service:Service = new Service(gatewayUrl, null, 'hiworld', null , null);
//var pc:PendingCall = _service.returnResultset("Hello World");
var pc:PendingCall = _service.returnResultset();
pc.responder = new RelayResponder(this, "handleResult", "handleError");
function handleResult(re:ResultEvent)
{
trace('The result is: ' + re.result);
}
function handleError(fe:FaultEvent)
{
trace('There has been an error');
}
<?php
class hiworld
{
function hiworld()
{
$this->methodTable = array
(
"returnResultset" => array
(
"access" => "remote",
"description" => "Pings back a message"
)
);
$dbhost="localhost";
$dbuser="root";
$dbpass="abdef3fedbalkj";
$dbconnect=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("awards", $dbconnect);
}
function returnResultset()
{
// return 'You said: ' . $sMessage;
return mysql_query("SELECT * FROM teacher");
}
}
?>
i kinda know what i'm missing... i just don't know how or where to fill it in.. flash traces "The result is: [object Object]".... could somebody tell me where i'm going wrong?
pappudj
October 10th, 2006, 08:56 PM
i donot have any noleage about php
Mak Valley
October 10th, 2006, 09:01 PM
then that has to be the most useful post of the day! thanks.
ghjr
October 10th, 2006, 10:54 PM
almost there bomsaway.
since your returning the query to flash, you need to cast it to a RecordSet, like so:
import mx.remoting.RecordSet;
function handleResult(re:ResultEvent)
{
var myRecordSet:RecordSet = RecordSet(re.result);
var result_array:Array = myRecordSet.items;
for(var i:Number = 0; i < result_array.length; i++) trace(i);
}
That would just output the number of results it found. Don't forgot to import the RecordSet class, you can do that together with the other remoting imports.
There are tons of stuff you can do with RecordSets, so read up on it on the web (the link that I sent should be a good start) and in the Flash help.
Cheers,
ghjr
Mak Valley
October 11th, 2006, 05:57 PM
edit: i can count rows now!!!!!!!!
thank you ghjr, you've been extremely helpful!
Mak Valley
October 11th, 2006, 06:14 PM
var colnames = myRecordSet.getColumnNames;
for (i=0; i < result_array.length; i++){
//trace(myRecordSet.getItemAt(i).colnames[i]);
trace(myRecordSet.getItemAt(i).student);
}
the uncommented trace statement traces all the records in the "student" row... just what i want it to do... the commented one traces "undefined".. my question is: do i need to type out the loop through each row, or can i do it with "colnames" like i'm trying to?
ghjr
October 11th, 2006, 11:03 PM
You should be able to do it with colnames, but there would need to be another for loop in there to loop through columns as well as rows, something like:
var colnames_array = myRecordSet.getColumnNames();
var result_array:Array = myRecordSet.items;
for (i=0; i < result_array.length; i++){
for (p=0; p < colnames_array.length; p++) {
trace(myRecordSet.getItemAt(i).colnames_array[p]);
}
}
For some reason that's returning undefined, so I'm probably doing something wrong heheh. I'm pretty new to remoting as well, but I'll take a deeper look into this and update with an answer. For now you could just hardcode the column names in if this is a small project, for example:
for (i=0; i < result_array.length; i++){
trace(myRecordSet.getItemAt(i).student);
trace(myRecordSet.getItemAt(i).teacher);
trace(myRecordSet.getItemAt(i).grade);
}
Mak Valley
October 12th, 2006, 07:53 PM
thank you soo much ghjr, i probably would've jumped off a bridge already if it weren't for you.. anyways, new question:
lets say i have this line on the php page which i'm "remoting"
return mysql_query("SELECT * FROM $tablename")
where $tablename is a GET variable... obviously i have a bunch of tables and don't want to make a remoting service for each table... so, is it possible to achieve this?
edit: possible solution?
var _service:Service = new Service(gatewayUrl, null, 'slideshow', null , null);
that maps out to my services.... i wanted to try slideshow.php?tablename=mytable to make the GET variable work.. but, it doesn't seem like it would work to me as amfphp appends the extention to the end... is there a way with this method as well?
ghjr
October 13th, 2006, 11:04 AM
You almost got it... what you need to do is actually pass the table names as parameters of the remote method call, and not when you establish the connection to your service.
So for example, in flash you would have:
var _service:Service = new Service(gatewayUrl, null, 'slideshow', null , null);
var pc:PendingCall = _service.yourMethodNameHere(yourTable);
and in 'slideshow' service:
function yourMethodNameHere($myTable) {
// previous db connection code, then:
return mysql_query("SELECT * FROM $myTable");
}
Don't jump off a bridge... it's already friday, enjoy the weekend. :)
Cheers,
ghjr
Mak Valley
October 13th, 2006, 06:24 PM
that makes sense, but what i'm really trying to do is pass a php GET variable to flash... know how to do this?
edit: to anybody who may ever need this information i've figured it out!
code the GET variable when embedding flash into your page....
<embed src="index.swf?flashvar=<?php $_GET['phpgetvar'] ?>"><param...
then to call it in flash.. you simply use something like
_root.test_txt.text = _root.flashvar;
lets say the swf file is embedded on index.php... if you were to go to index.php?phpgetvar=hawaii, test_txt would say "hawaii"....
note* file must be published for this method to work.
i'm not sure if this is the best/safest way, but it works!
thanks again ghjr..
ghjr
October 14th, 2006, 11:41 AM
Yup, you could do that, or use FlashVars (safety-wise it's the same, but there are some benefits to using it, so it's the recommended approach):
http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_16417
Cheers,
ghjr
Mak Valley
October 17th, 2006, 06:35 PM
question: if i'm returning multiple querys from multiple tables like
return mysql_query("SELECT * FROM thistable");
return mysql_query("SELECT * FROM thattable");
return mysql_query("SELECT * FROM anothertable");
will it all be in the same recordset? or will it not work that easily?
ghjr
October 17th, 2006, 08:00 PM
The return statement will stop the execution of the function and will return whatever is passed on (in your case the MySQL query). So in the code above it would only return the query from thistable.
You could probably optimize this to actually return the results from all three tables from just one query call. Maybe by seperating the table names in the query by comma's would work, not sure though.
Cheers,
ghjr
Mak Valley
October 17th, 2006, 08:03 PM
i guess it doesn't work that easy... thanks.
Mak Valley
October 19th, 2006, 06:00 PM
nevermind
Mak Valley
November 2nd, 2006, 07:30 PM
ok, i made a slideshow that works perfectly on my localhost.... i uploaded my project to my server at work (and it worked)... went to lunch, came back... and now it won't even connect to the gateway on the server... when i view the window activity (in safari) it says it's connecting to gateway.php which is something like 35 megabytes! safari either crashes before it loads or takes 5 minutes to load the gateway then tells me blah flash is blah blah running a script that may make my computer unreblahsive.... i made only a few modifications to CERTAIN variables (only ones partaining to information being passed from the database)
so, could this be because of my server? or changes i made?
edit: in the activity window while viewing the project on my localhost it says my gateway is 2.2 kb... please tell me somebody knows how there could be such a dramatic difference (filesize) between the two!
Mak Valley
November 2nd, 2006, 08:36 PM
nevermind, figured it out... thanks for reading though
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.