Database grabs with Flash MX04

Hi.

I’m looking to pull text from a database and display it in various places in Flash. I was also like to be able to format the text with CSS.

What is the best way to do this?
Its a MySql db, and I’m aiming to use PHP as well since I know it.
I’ve seen people that create XML docs from PHP, but I’m wondering if it would be worthwhile to do this or not.

Can somone run down the best way to do this?

I guess I could do simple text files as well, but I’m sort of attached to doing the database design.

Howdy… :slight_smile:

If you do not need to save the result to somewhere else(XML or Text file) then there is absolutely no reason to generate one, unless if you want to slow down the processing speed intentionally…

Just have PHP file output the value from the database that Flash can understand…
Flash understands the output format of ‘variable1=value1&variable2=value2’… :slight_smile:

ok,

Would I be able to format the data coming in as well with CSS?
Also, can you link to an external CSS file from inside Flash?

Well… It depends on how you define ‘format’… But I’d say leave it up to the Flash for the formatting part since Flash is slow on processing the string data…

As for the CSS, I have no clue since I don’t use FMX 2004…

Im talking about making part of the text that is getting pulled from the database bold or italic, etc.

Bold and/or italic doesn’t need CSS, you can do that with html-enabled textareas.
But yes, you can also use CSS in FMX04, either internally, or by loading the stylesheet.
Check out the tutorials here and Flash Help, everything is in there.

Right i knew you could do it with HTML tags, but that seems redundant.

I started trying to pull from my DB in Flash, but it doesnt’ seem to be working.

Can anyone see a problem with this code?


  
  <?php
  //read.php 
  //prints contents of content column where filename = $file
  
  $host = "localhost";
  $user = "flash";
  $pass = "****";
  $dbName = "flashphp";
  $tableName = "flashText";
  
  
  // Attempt to connect to MySQL server
  
   $link = mysql_connect($host, $user, $pass) or die("fiddlesticks: " . mysql_error());
    mysql_select_db($dbName);
    
    $result = mysql_query("SELECT content FROM $tableName1 WHERE (name=$_GET[file])");
   
    $desiredContent = mysql_fetch_array($result, MYSQL_ASSOC);
  
    mysql_free_result($result);
  
    print "bioText=$desiredContent[bioText]";
  
    mysql_close($link);
  ?>
  

That’s the PHP file obviously… and the Actionscript is…


  onClipEvent (load) {
    loadVariables("http:/localhost/bio.php?file=bio", this, "GET");
  }
  

The text areas’s variable name is “bioText”.
The db has 2 fields, “name” and "content"
So any red flags??

Uh, I don’t know why you use onClipEvent and loadVariables() in FMX 2004, but try this… Remove that movieClip script and put this into the frame…


bio_lv = new LoadVars();
bio_lv.file = "bio";
bio_lv.onLoad = function(ok)
{
	 if (ok)
	 {
		 trace("Finished loading data from the server...");
		 trace("this.name = " + this.name);
		 trace("this.content = " + this.content);
		 trace("this = " + unescape(this));
	 }
	 else
	 {
		 trace("Problem accessing server...");
	 }
}
bio_lv.sendAndLoad("http:/localhost/bio.php", bio_lv, "POST");

and post the trace output if it does not work…

<?php
  //read.php 
  //prints contents of content column where filename = $file
  
  $host = "localhost";
  $user = "flash";
  $pass = "****";
  $dbName = "flashphp";
  $tableName = "flashText";
  
  
  // Attempt to connect to MySQL server
  
   $link = mysql_connect($host, $user, $pass) or die("fiddlesticks: " . mysql_error($link));
    mysql_select_db($dbName);
    
    $result = mysql_query("SELECT content FROM $tableName1 WHERE name='" . $_GET['file'] . "'", $link) or die ("Query Error: " . mysql_error($link));
	$desiredContent = mysql_fetch_assoc($result);
    print "&bioText=" . $desiredContent['bioText']; //doesn't hurt to just have the amp;  
    mysql_close($link);
  ?> 

[AS]bio_lv = new LoadVars();
outgoing_lv = new LoadVars();
outgoing_lv.file = “bio”;
bio_lv.onLoad = function(success)
{
if (success)
{
trace(“Finished loading data from the server…”);
trace("this.name = " + this.name);
trace("this.content = " + this.content);
trace("this = " + unescape(this));
}
else
{
trace(“Problem accessing server…”);
}
}
outgoing_lv.sendAndLoad(“bio.php”, bio_lv, “POST”);//use relative paths![/AS]

When having problems like this, tracing functions are your best friend. In flash, you can use trace() or you can list all the variables/objects, in php you can just print to the page. Tracing will help you narrow your problems quickly.

Oh, dumb me… Yeah… It’s got to be sendAndLoad() function… :goatee:

Also, I noticed you are using variable names for your text boxes, but i’d suggest using an instance name and:

myTextBox.html = true;
myTextBox.htmlText = "<B>your text</B>";

Ok,
questions…

  1. You’re using $_Get in PHP and $_POST in FLash. Isnt that a mistake??
  2. Im not getting what you mean by replacing the variable name with an instance and the AS above.
  3. Can you comment any more on the AS? There’s a few things I’ve never seen.
  1. Yes. (see code below)
  2. Don’t assign a Variable Name, assign an instance name. Look around the text-box properties and you’ll see what I’m saying.
bio_lv = new LoadVars(); //create an object for the returned data.
outgoing_lv = new LoadVars(); //create a Sepereate object for the variables you're passing to the PHP
outgoing_lv.file = "bio"; //assign outgoing variables
//same as sending: ?file=bio
bio_lv.onLoad = function(success){
     if (success){ //success will be true if there was no problem in the accessing the PHP file
	 	_parent.myTextBoxName.html = true; //enables html tags to be placed in the text box
		_parent.myTextBoxName.htmlText = this.bioText; //assigns the value of bioText to the text box 'myTextBoxName'
     }else{
          trace("Problem accessing server...");
     }
}
outgoing_lv.sendAndLoad("bio.php", bio_lv, "POST");//use relative paths!
<?php 
  //read.php 
  //prints contents of content column where filename = $file 
   
  $host = "localhost"; 
  $user = "flash"; 
  $pass = "****"; 
  $dbName = "flashphp"; 
  $tableName = "flashText"; 
   
   
  // Attempt to connect to MySQL server 
   
   $link = mysql_connect($host, $user, $pass) or die("fiddlesticks: " . mysql_error($link)); 
    mysql_select_db($dbName); 
     
    $result = mysql_query("SELECT content FROM $tableName1 WHERE name='" . $_POST['file'] . "'", $link) or die ("Query Error: " . mysql_error($link));  //POST, my bad
    $desiredContent = mysql_fetch_assoc($result); 
    print "&bioText=" . $_POST['bioText']; //doesn't hurt to just have the amp; POST, my bad
    mysql_close($link); 
  ?>

All the variables returned from the php page are stored in the LoadVars Object, bio_lv. For example, if the returning query string or post data is:

?Name=Norie&Occupation=Pimp&Honesty=false

But when loaded into flash using (sendAndLoad or load) produces:

loadVars_object.Name //will return 'Norie'
loadVars_object.Occupation //will return 'Pimp'
loadVars_object.Honesty //will return 'false'

Alrighty round 3 :stuck_out_tongue:
hehe
Still not getting it to work…

  1. There’s a syntax error in the SELECT.
    I changed it to

 $result = mysql_query("SELECT content FROM $tableName1 WHERE (name=$_POST[file], $link)";
 

btw why is $link in there anyway??

I’m assuming these


 _parent.myTextBoxName.html = true; //enables html tags to be placed in the text box
_parent.myTextBoxName.htmlText = this.bioText; //assigns the value of bioText to the text box 'myTextBoxName' 

should be:


 _root.bioText.html = true; //enables html tags to be placed in the text box
 	   _root.bioText.htmlText = mc_background.bioText; //assigns the value of bioText to the text box 'myTextBoxName'
 

Otherwise I dont see anyplace else in the script where the Flash text area is targeted.

  1. Is this all to be placed on the root? or on a movieclip with the text area in it?

Thanks for the continued help… i really appreciate it :slight_smile:

Post your files.

$link is a pointer to the active connection.
2)Good Assumption.
3)Doesn’t matter. Just make sure you are using the correct variable scope.