Actionscript, php, mySQL dilemma. Please help!

I’m not sure I’m going about this the right way but is there a way to get a mySQL database row id from the address bar url (…com/script.php?id=5) into a swf file? I’ve read all the books, forums, help files, tuts and tried everything I can think of but can’t find an answer. It seems like a pretty basic thing to do but I can’t make it work.

The php script outputs the correct data in the format Flash requires (&data1&data2&etc.) with a url that ends like the one above. I need to have the swf file get the url with the particular id number and read the data it’s putting out. Right now the only way I can make it work is to hard code the full url with the id number in the actionscript like so:

cardVars = new LoadVars();

cardVars.load("http://www.....com/script.php?id=5", 0, "POST");

	cardVars.onLoad = function(ok) {
		if (ok) {
			data1 = this["data1"];
			data2 = this["data2"];
			data3 = this["data3"];
		} else {
			trace("Error loading data");
		}
	};

Is there a way to call the url or just the particular id number with the actionscript when the php script calls a different row from the database and the id number changes?

If anyone can help me out here, I’d really appreciate it.

Tom

When you’re writing out the FlashVars with PHP, you’ll need to echo out the values, not included the PHP variables. Like so:

echo '<param name=flashVars value="id=' . $id . '&field1=' . $data1 . '&field2=' . $data2 . '&field3=' . $data3 . '">';

I have a simple article on my site about FlashVars which might help you, although I think you might be beyond it by now. Take a look if you like:

http://fcOnTheWeb.com/articles/flashvars/

Thanks for responding Eastcoast but I’m starting to feel like I’m on a quest for a non-existent Holy Grail! After Googling flashVars, I’m more confused than ever.

Right now, my php script (http://www…com/script.php?id=5) looks like this:

// Connect to the database
include_once("dbconnect_database.php");

// Request the data
$id = $_GET['id'];
$result = @mysql_query("SELECT id,data1,data2,data3 FROM data_table WHERE id='$id'");
if (!$result) { 
	exit('<p>Error fetching data_table: ' . mysql_error() . '</p>'); 
}

$result = mysql_fetch_array($result);
$id = $result['id'];
$data1 = $result['data1'];
$data2 = $result['data2'];
$data3 = $result['data3'];

echo "id=$id&field1=$data1&field2=$data2&field3=$data3"; //which outputs the correct string when run by itself.

// Request the swf file
$id = $_GET['id'];
$swf = @mysql_query("SELECT id,imagepath FROM swf_table WHERE id='$id'");
if (!$swf) { 
	exit('<p>Error fetching swf: ' . mysql_error() . '</p>'); 
}  

// Display the swf file
$swf = mysql_fetch_array($swf);
$id = $swf['id'];
$imagepath = $swf['imagepath'];
	
include ($imagepath);

and the embedding code with the flashvars added like so…


<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100%" height="100%" id="mySWF" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="mySWF.swf" />
<param name=flashVars value="id=$id&field1=$data1&field2=$data2&field3=$data3">
<param name="loop" value="false" />
<param name="quality" value="high" />
<param name="bgcolor" value="#006666" />
<embed src="mySWF.swf" flashVars="id=$id&field1=$data1&field2=$data2&field3=$data3" loop="false" quality="high" bgcolor="#006666" width="100%" height="100%" name="mySWF" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</embed>
</object>

… and which is probably wrong as far as the values are concerned since all I’m getting in the swf flash file are $id, $data1, etc. or nothing at all depending on the actionscript I include. I really have no idea what the actionscript needs to be for flashVars.

Can you help?
Thanks,
Tom

You need to use flashvars. The php script can change the embedding html code to add these flashvars which are then passed to the swf when it loads.

Thanks Chris. I think I found the solution in your answer to the comment at the bottom of your page with the article about flashvars. All I really needed to get was the id from the url of the php page and

<param name=flashVars value="id=<?php echo $_GET["id"]; ?>">

in the embedding code did just that. From there, using this actionscript

cardVars = new LoadVars();
if (id != "") {
	cardVars.load("http://www.domain.com/script.php?id="+id, 0, "POST");

	cardVars.onLoad = function(ok) {
		if (ok) {
			id = this["id"];
			data1 = this["data1"];
			data2 = this["data2"];
			data3 = this["data3"];
			} else {
			trace("Error loading data");
		}
	}
};

with the same php script as in my previous post did the trick. So simple, and yet so obscure.

I’m embarrassed to admit how long I’ve been trying to do this. May the gods smile on you! I’ve bookmarked your website for future reference.

Thanks Again,
Tom