Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting Flash to a database

Step-by-step tutorial

Connecting Flash to a database

by  wangbar  Posted    (Edited  )
Most dynamic data websites and applications consist of three tiers. In the case of a Flash site tier 1 is your Flash movie. It communicates with a serverside script (written in PHP or ASP etc) which is the second tier. The third tier is the database which holds the information we want to work with.

The Flash movie uses loadVariables or the LoadVars object to send and receive data. There is a sample .fla to download here - http://www.wangbar.co.uk/tek-tips/dbAccess.fla which contains a form wired up to a script and database. Here is the script the movie uses to get information from the user, format it for the serverside script (written in PHP) and receive information back from the script:

Code:
talkToDB = function (email, messageBox) {
	// set up LoadVars Object
	lv = new LoadVars();
	// this function is triggered when the data is sent back from DB
	lv.onLoad = function() {		displayReturned(this.email, this.messageBox);
	};
	// format datastring and send to database
	lv.email = email;
	lv.messageBox = messageBox;
	lv.sendAndLoad(DBPATH, lv, 'POST');
};
displayReturned = function (email, messageBox) {
	// show data returned from PHP script
	returnedEmail.text = email;
	returnedMessage.text = messageBox;
};
//path to php script & database
_global.DBPATH = 'http://www.wangbar.co.uk/php/get_tektips.php';
//button event
sendButton.onRelease = function() {
	talkToDB(yourEmail.text, yourMessage.text);
};
//
stop();

You should be able to test this movie directly from the authoring environment as there are no security implications with accessing remote scripts as there are when the movie is running online.

The PHP looks like this:

Code:
<?
#connect to database (separate script)
include 'connect.php';

#retrieve variables from Flash
$email=$_POST[email];
$messagebox=$_POST[messagebox];

#insert values into the database
$query="INSERT INTO tektips (email,message,date) VALUES ('".$email."','".$messagebox."',NOW())";

#check that the procedure ran properly otherwise throw error
$result=mysql_query($query) or die("mysql error while trying to run <b>$query</b>:".mysql_error());

#format and return variables to Flash
$returnedData="&email=$email&messagebox=$messagebox&";
echo $returnedData;
?>

It takes the info sent from the Flash movie, sticks it in the database and then returns the information you sent back to Flash (it should appear in yellow on your screen).
A very basic script, but using other SQL commands you can also retrieve data from the DB, update it etc...
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top