View Full Version : Bringing in DataBase info into Flash?

Web Guy
04 April 2003, 05:18 PM
I work for and I am working on bringing in info from our database into my flash movie. Below is the start loop to end loop code.

<FORM ACTION="/cgi-bin/sgin0101.exe" METHOD="POST">
<TD WIDTH="65">
<INPUT TYPE="hidden" NAME="FNM" VALUE="00">
<INPUT TYPE="hidden" NAME="T1" VALUE="!+ITNO!">
<INPUT TYPE="image" NAME="Details" SRC="!+JPGs!" BORDER="0" WIDTH=60 HEIGHT=60>
<DIV ALIGN="LEFT"><FONT SIZE="1"><A CLASS=black_nobold HREF="/cgi-bin/sgin0101.exe?FNM=00&UID=!+USID!&GEN0=!+GEN0!&GEN1=!+GEN1!&GEN2=!+GEN2!&GEN3=!+GEN3!&GEN4=!+GEN4!&GEN5=!+GEN5!&GEN6=!+GEN6!&GEN7=!+GEN7!&GEN8=!+GEN8!&GEN9=!+GEN9!&TRAN85=Y&T1=!+ITNO!&UREQB=1&UREQA=2&UREQC=3&UREQD=4" NAME="info">!+IDES!</A>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif"><B><FONT COLOR="#CC0000"><BR>
<A CLASS=black_nobold HREF="/cgi-bin/sgin0101.exe?FNM=00&UID=!+USID!&GEN0=!+GEN0!&GEN1=!+GEN1!&GEN2=!+GEN2!&GEN3=!+GEN3!&GEN4=!+GEN4!&GEN5=!+GEN5!&GEN6=!+GEN6!&GEN7=!+GEN7!&GEN8=!+GEN8!&GEN9=!+GEN9!&TRAN85=Y&T1=!+ITNO!&UREQB=1&UREQA=2&UREQC=3&UREQD=4" NAME="info">more
info</A> | <A CLASS=black_nobold HREF="/cgi-bin/sgic0101.exe?FNM=00&ITN=!+ITNO!&UID=!+USID!&GEN0=!+GEN0!&GEN1=!+GEN1!&GEN2=!+GEN2!&GEN3=!+GEN3!&GEN4=!+GEN4!&GEN5=!+GEN5!&GEN6=!+GEN6!&GEN7=!+GEN7!&GEN8=!+GEN8!&GEN9=!+GEN9!&TRAN85=Y&QTY=1" NAME="buy">buy
now</A> </FONT></FONT></FONT></DIV>
<TABLE ALIGN="center" VALIGN="middle" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0" BGCOLOR="#000000">
<TD BGCOLOR="#FFFFFF" HEIGHT="7" ALIGN="center"><IMG SRC="/gfx/spacer.gif" WIDTH=0 HEIGHT=0></TD>
<TABLE ALIGN="center" VALIGN="middle" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0" BGCOLOR="#000000">
<TD BGCOLOR="#000000" HEIGHT="1" ALIGN="center"><IMG SRC="/gfx/spacer.gif" WIDTH=0 HEIGHT=0></TD>
<TABLE ALIGN="center" VALIGN="middle" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0" BGCOLOR="#000000">
<TD BGCOLOR="#FFFFFF" HEIGHT="7" ALIGN="center"><IMG SRC="/gfx/spacer.gif" WIDTH=0 HEIGHT=0></TD>

And help on bringing this info into my flash movie would be great. At this point I can pass our variables thru the flash movie and back out but have not been able to get this kind of info to work. PLEASE HELP!!

Thanks guys!!

Basically the above will set a product loop and at this point I would settle with bringing in one item and not even a loop from the database.

04 April 2003, 01:06 AM
First off you cannot pass in complicated HTML markup like that, flash is somewhat limited in it's capacity to parse html. Your best bet is to use one of three built in methods for retrieving data from external sources.

1. loadVariables()
2. loadVars()
3. loadXML()

The first method listed is okay but it's been over shadowed but the last two. loadVariables gives you the ability to load in external data in the form of name/value pairs.

The second method is new to Flash MX, this too loads data in name/value pairs but it also has a number of built in methods that allow you to determine events etc. the Third method is tried trusted and true, in fact loadVars() is based on the same methods as loadXML.

loadXML () (if you have any experience with xml - bloody easy to learn if you don't) lets you load in data from an xml file that can be generated from your database (depending on the language you use). The XML is perfect and easy to parse, in fact there are methods that are created to help you to that extent.

To summarize: you've got a few means to populate your flash movie with external data. Search the power of google!!

04 April 2003, 01:45 AM
I'm actually working on a site right now that consists of a Flash MX front end to a MySQL db backend. It's not that tough once you get the hang of it, but there's no way that particular HTML code will pass directly into Flash. If you're using PHP (I am, so I'll explain in terms of php - you can fit it to your language of choice), create your form in Flash. Then, as xenxen suggested, use LoadVars() objects. You can assign the input field from the Flash form to the LoadVars() object, use a sendAndLoad() action pointed at the php page, back into the current or a different LoadVars(), method either POST or GET. Code your script as you would if your were dealing with straight HTML, then urlencode your return variables in echo or print lines. These lines are read directly back into Flash as name/variable pairs, where you can access each as a property of the LoadVar() object.

A great resource for this type of thing is the forums at - they're a bunch of great guys and gals over there, and quite prompt with replies to questions.


p.s. - if you need or want any further clarification or commiseration, feel free to post here or e-mail me with a relevant subject line (most of the mail I get at the address listed here is spam, so it goes away quickly....)

04 April 2003, 02:23 PM
Just one quick tip for anyone using the loadVars() Method the sendAndLoad() method is all well and good - but I've found it to be particularly unreliable and a bit of a nusiance to code. Sometimes it will act as it should other times it just seems to break (believe me I've spent countless hours trying to research this problem) so the best thing you can do for yourself is just use the load() method and then create a handler function for the onLoad() event. You can find out more on forums like or Oh and one more thing, really look at the loadXML() object and all it's methods before you code yourself into a hole... it might save you some time in the long run.

04 April 2003, 02:50 PM
xenxen- out of curiosity, what issues have you been having with sendAndLoad? I've actually found it to be mostly stable - one or two minor caveats aside. :shrug: And have you found any particularly good tuts on loadXML? Personally, I've found that parsing the loaded xml data is quite the pain in the butt - but I think I'm missing a step somewhere. Just never seem to have gotten to work right more than twice in a row.

04 April 2003, 03:30 PM
what I've noticed mostly is that the method sendAndLoad() is rather finicky depending on the webserver that it's being hsoted on, it could be something as stupid as they way they set up the mime types or sometihng like that but the methods work okay on a testing environment. What I tend to do regardless is to avoid all of that altogether by warpping it all into a function:

function doSomething(urlToPost)
strQueryString = "name=" + value // continue this as much as you want
myLoadVars = new LoadVars();
myLoadVars.onLoad = function(success)
trace ("SUCCESS");

myLoadVars.load(urlToPost + strQueryString, "POST");

// create method to trace progress of load method

What i end up doing is call the load() method and atttach a querystring to the url (works perfectly every time) this way on my ASP/PHP or whatever script I can write a response. its simple and I haven't had a problem working this way.

Web Guy
04 April 2003, 03:38 PM
Can some one show me how about would I go doing this a little more detailed? Some examples? Any help is much appreciated..I have to pull this data via a windows server..This is for!!

04 April 2003, 03:51 PM
xenxen - I hadn't thought of concantenating the query to the URL string - interesting method! I'll have to keep that in mind if this sucker doesn't work after I upload it. I usually just add a variable into the LoadVars object from my form and use that in my PHP coded SQL query. The only "workaround" that I've found I have to do is re-assign the $_POST variables to local vars in the PHP script. Of course, my site's not live yet, so hopefully it'll work when it is... Otherwise, :banghead:

webguy - What exactly are you trying to do with the script? Looking at your posted code, the variable names seem a bit general and numerous, which makes it hard to follow without an idea of the intended output. (Or, I might just need more coffee.....) And an excellent resource is the previously mentioned - quite a few tutorials and examples, as well as pretty active forums.

04 April 2003, 05:47 PM
Webguy, I'll give you an example in pseudo code to get you started, you can look online at other forums for exact code, some of this is quite textbook and should be easily found anyways.

Method: loadVars() approach for getting db info into flash. Note that the scripting language (this is the language you use to communicate with your database etc.) can be anyhting like PHP or ASP etc.

In your external script (I'll use ASP since you are on windows IIS):
<%@ Language="VBSCRIPT"%>
<% Option Explicit %>
' decalre variables
Dim myRS Recorset object
Dim strCN 'Connection string
Dim stSQL 'SQL statement to
Dim myRS
Dim strDelimiter

'database connection
set myRS = Server.CreateObject("ADODB.Recordset")
myRS.ActiveConnection = strCN
myRS.Source = strSQL
myRS.CursorType = 0
myRS.CursorLocation = 2
myRS.LockType = 3

'retrive records form DB and put them into name/value pairs.
strDelimiter = "&"
'generate your name value pairs
'good practice is to use the names if your fields as the
'name of your variable'
'Dont forget to concatenate your name/value pairs with an "&"
'ID=ID & strDelimiter
'FIELDONE= FIELDONE & strDelimiter 'and so on

'once your vars are populated close th connection
'destroy the object
myRS = Nothing

In Flash (first frame or external *.as file):
// this method is used to load data from your ASP script.
// provide a URL and optional params in the form of a
// query string (see my previous posts) to retrieve the data.
function loadExternalVars(urlToLoad)
// instantiate a new loadVars() object
myLoadVars = new LoadVars();

// event triggered when data is loaded
myLoadVars.onLoad = function(success)
// this is to check for your loaded data
if (success)
trace("successfully loaded");
// if data is loaded successfully
trace("Failed to load");
// create methods to handle load errors.

// you can attach any querystrings you might need to commuicate with the script.
// this is for recordset paging or posting to a email script etc.


// this method parses the loaded data
// this is only ONE way of doing it, it's textbook and rather easy
// other methods might be to parse the variable: dataToParse using
// string functions, but this can get messy.
function parseLoadVars(dataToParse)
// create methods for parsing data
// a common practice is to create an array to hold the variables
// I don't really like making objects like this a global
// but for now it's okay
// when you start to learn to program in more object oriented ways
// you won't have to do this
_global.myDataContainer = new Array();

// populate the container array
_global.myDataContainer[0] = dataToParse.ID // see the asp script for reference
_global.myDataContainer[1] = dataToParse.FIELDONE // see the asp script for reference

// optionally you can create a method to retrieve the data
function getData(index)
// returns the value of the array that you requested
return myDataContainer[index];

that's really all there is to it. this same approach can be applied to using the loadXML object, XML in flash have more methods to help parse and split the data. fun stuff once you get the hang od it.

05 May 2003, 06:52 AM
make sure your variables come in as a URL-encoded format with no carriage returns. Whether it's ASP or PHP... otherwise Flash won't understand what ur trying to do.

i.e. myVar1=hello&myVar2=hello2&myVar3=hello3

05 May 2003, 02:16 PM
Yeah I forgot that part in the ASP - AlWAYS URL encode your strings/vars when pulling data into flash... this is true for XML as well, any text nodes should be URL encoded (you can leave the actual markup alone, believe me this helps a hell of a lot when parsing the XML data.

Web Guy
05 May 2003, 02:56 PM
Thanks for all of the help guys. I am passing our variables right now thru flash using the get method and such it and works fine. But I am only passing variables to a link and have not brought in forms and such into it. Thanks again and today while at work I am going to test a few things..:beer:

CGTalk Moderation
01 January 2006, 12:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.