PDA

View Full Version : MySQL > PHP > Flash


Kenzo
04-30-2006, 10:32 PM
hey i need help here im a bit of a newbie when it comes to this!

basically i have two fields in my MySQL table: header and details.


i've connected to the MySQL through PHP no problems and when i run a general query it comes up as a list no problem.

all i wanna do is now is get the info from the header and details field shown into 2 textboxeson my flash file.....how do i do that?? i know i gotta use a sendandload feature but im stuck plz help!!
:confused: :confused: :confused:

Cota
05-01-2006, 12:52 AM
Ok, first things first. Will Flash be seding data to this PHP script to process? Secondly, whats your current PHP code, and third, do you have any Actionscript already written, if so, post it.

Quick example:

var myVars:LoadVars = new LoadVars();
myVars.onLoad = function(success){
if (success){
trace("PHP File ran successfully");
//more code will go here to get the vars from PHP
}
}
myVars.SendAndLoad("file.php", this, "POST");

Kenzo
05-02-2006, 07:43 PM
Hey!

basically, i've been able to get data from mySQL via PHP
but i got two new problems now

my PHP code is this at the moment :

<?php

//connect to your database
$db = mysql_connect("localhost", "fiendcom","kOgydEAGi");

//make sure you are able to connect
if( mysql_select_db("fiendcom",$db) ){
echo "connection to FIEND DATABASE sucessful";
}else{
echo "connection to FIEND DATABASE failed";
}


//perform query
$result = mysql_query("
SELECT
header,details,type
FROM
fiendnews
ORDER BY
news_idno
DESC
LIMIT 1",$db);


// put that result into an array
if($myarray = mysql_fetch_array($result)){
echo "<br /><br />query worked";
}else{
echo "<br /><br />query failed";
}



//display results
echo "<br /><br /><br />Title: ".$myarray["header"]."<br/>".
"Message: ".$myarray["details"];



?>

in my mySQL table i have 3 sets of data with the fields
header and details for example :


header details

1/1/06 site worked
2/6/06 site worked again
3/9/06 site crashed!!!

and i have 2 dynamic text boxes, header and details
and what i wanna do is have a button so when its clicked
the text boxes show the next row 1/1/06, etc , then 2/6/06 etc
any ideas???

ALSO, i need to get images from mySQL to flash via PHP...any idea?
and how do u store images in myQL anyways??????

help!!!! x

Cota
05-02-2006, 09:41 PM
Basic logic...loop through the database results outputting each record colum one by one to Flash. I have an ASP example, but that wont help you much. I will try and build you a sample, but wont have it much later..time is tight at the moment.

invader
05-02-2006, 10:28 PM
mysql doesn't store images. typically, what you would do is save the image in a directory on the web server, and save the path to it in mysql. that means you'll be adding another field.

now, this is my personal opinion.. sendandload has its limitations and is usually more ideal if you have a form to submit and/or just need one variable or one set of variables to be returned. if you think you might have multiple sets returned and have a limited number of variables to pass (for example: item=pc&minprice=100&maxprice=1000), i think using xml would be your best bet.

one thing to keep in mind either way is that the syntax of the php script's outuput is very strict. sendandload expects the output to be in query string format (like the item=... above) and xml requires standard xml formatting. if your script echos line breaks "<br>", status, or anything else, it could easily fail trying to retrieve the data.

the main reason i recommend using xml is that you can run one mysql query, then loop through the results and output them all in one request to the server. there are plenty of good xml->object actionscript functions out there, too.

hopefully this helps you get in the right direction

Kenzo
05-02-2006, 11:52 PM
mysql doesn't store images. typically, what you would do is save the image in a directory on the web server, and save the path to it in mysql. that means you'll be adding another field.

now, this is my personal opinion.. sendandload has its limitations and is usually more ideal if you have a form to submit and/or just need one variable or one set of variables to be returned. if you think you might have multiple sets returned and have a limited number of variables to pass (for example: item=pc&minprice=100&maxprice=1000), i think using xml would be your best bet.

one thing to keep in mind either way is that the syntax of the php script's outuput is very strict. sendandload expects the output to be in query string format (like the item=... above) and xml requires standard xml formatting. if your script echos line breaks "<br>", status, or anything else, it could easily fail trying to retrieve the data.

the main reason i recommend using xml is that you can run one mysql query, then loop through the results and output them all in one request to the server. there are plenty of good xml->object actionscript functions out there, too.

hopefully this helps you get in the right direction


hey thanks for that

i've already made an extra field in mySQL for the image as a path.
really i may not even need to use PHP,
if i can get the image path from mySQL to flash, then i'd be happy with that
i cant use XML now tho but i may later on after this....its a secret why tho!

Cota
05-02-2006, 11:55 PM
In my opinion, XML takes longer to parse and can be bulky. Anyway, what are the colum names that are being returned by the query?

Kenzo
05-03-2006, 12:51 AM
In my opinion, XML takes longer to parse and can be bulky. Anyway, what are the colum names that are being returned by the query?

just :

id
description
imagelink

x

Cota
05-03-2006, 01:13 AM
So your PHP should look something like this

<?php

//connect to your database
$db = mysql_connect("localhost", "fiendcom","kOgydEAGi");

//make sure you are able to connect
if( mysql_select_db("fiendcom",$db) ){
echo "connection to FIEND DATABASE sucessful";
}else{
echo "connection to FIEND DATABASE failed";
}


//perform query
$result = mysql_query("
SELECT
header,details,type
FROM
fiendnews
ORDER BY
news_idno
DESC
LIMIT 1",$db);

$num=mysql_numrows($result);

// echo results to Flash
$i=0;
while ($i < $num) {
echo "&num=".$num;
echo "&id".i."=".mysql_result($result,$i,"id");
echo "&description".i."=".mysql_result($result,$i,"description");
echo "&imagelink".i."=".mysql_result($result,$i,"imagelink");
$i++;
}

Keep in mind I'm not really a PHP guy. There may be errors.
And the Actionscript side


var ids = new Array();
var descriptions = new Array();
var imagelinks = new Array();

myVars = new LoadVars();
myVars.onLoad = function(success){
if(success){
for(i = 0;i < this.num-1;i++){
daid = "id" + i; //Temp variable
dadescript = "description" + i; //Temp variable
daimage = "imagelink" + i; //Temp variable

ids[i] = this[daid];
descriptions[i] = this[dadescript];
imagelinks[i] = this[daimage];

trace(ids[i]);
trace(descriptions[i]);
trace(imagelinks[i]);

}

}

}
myVars.load("file.php");

Kenzo
05-03-2006, 09:03 AM
thanks cota.....im gonna try this

im gonna change the fields here

$result = mysql_query("
SELECT
header,details,type
FROM
fiendnews
ORDER BY
news_idno
DESC
LIMIT 1",$db);

to id, description and imagelink tho
and change the table name to the right name............

lemme try what u done tho x

Kenzo
05-03-2006, 09:19 AM
another question,

why trace?

shouldnt i load ids and descriptions into textfields
and images into movieclips??

Cota
05-03-2006, 12:50 PM
The trace is just for testing purposes, so what we're sure we're getting the correct info into Flash.

Kenzo
05-03-2006, 01:00 PM
The trace is just for testing purposes, so what we're sure we're getting the correct info into Flash.

cool

i just had a new idea!

basically, i've got the text data coming through to my site
what i was thinking is changing the files to .swf so
when it comes to the site the imagelink text reads as
"http://www.fiendcompany.com/images/Fiendiamt.swf"
in a textbox
is there a way where i can manipulte the text
so that i can load the swf using the loadmovie() method???????????

Cota
05-03-2006, 09:28 PM
whats wrong with the current method? Did you get it working?

Kenzo
05-03-2006, 10:07 PM
nah nothing comes happens!!

Cota
05-03-2006, 10:36 PM
are testing locally or online?

hooj
05-07-2006, 06:32 PM
In my experience with echoing variables from PHP to Flash you have to build your entire response string before echoing it.

// echo results to Flash
$i=0;
while ($i < $num) {
$echoString = "&num='$num'";
$echoString .= "&id=mysql_result($result,$i,'id')";
$echoString .= "&description=mysql_result($result,$i,'description')";
$echoString .= "&imagelink=mysql_result($result,$i,'imagelink')";
echo $echoString;
$i++;
}


Correct me if I am wrong, but I'm pretty sure the LoadVars object in Flash stops listening for responses once it receives data through a PHP echo command. For instance, if you had a simple PHP script:

<?php

echo "&test1=Goodbye";
echo "&test2=Cruel";
echo "&test3=World";
?>

You would only get the test1 variable back (or test3, I can't remember if it is the first or last echo that gets sent back to Flash).

OR I could be off my rocker entirely, and I just haven't been diligent in my testing.

Apparantly there is a FlashSQL extension available now that can access sql databases directly from Flash and you don't have to mess around with PHP.

Cota
05-08-2006, 03:49 AM
I use this format all the time

<?php

echo "&test1=Goodbye";
echo "&test2=Cruel";
echo "&test3=World";
?>

Works fine for me in PHP and ASP.

hooj
05-08-2006, 07:22 AM
Then I stand corrected!

Do you know anyone that's tried the FlashSQL plugin?

Kenzo
05-09-2006, 07:11 AM
are testing locally or online?

online1

Cota
05-09-2006, 08:05 AM
If your testing online, meaning the scripts are on a web server, then you wont see the traces at all, they are for local debugging...You can plus them into a textbox if you want.