To get the gist of it, what you'll need to do is poll a server side script, whether it's PHP, ASP, Python, whatever, to access the database.
What you'd do is from your Flash app, you'd send out the request with a timestamp based on the last request. Then your script will return only the new data. That way you're not clogging your tubes from always returning the entire chat log with every request since it will be polling frequently.
As for code, it's hard to just give you a script, but here's an extremely quick example. Completely untested and insecure, but it'll give you an idea:
PHP Code:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<?php
header('Content-type: text/xml');
$c = mysql_connect('localhost', 'username', '*****');
mysql_select_db('mydb', $c);
$query = "SELECT `message` FROM `messages` WHERE timestamp > {$_GET['timestamp']}";
$result = mysql_query($query, $c);
while($row = mysql_fetch_assoc($result))
{
echo '<m>'.$row->message.'</m>';
}
?>
</results>
PHP Code:
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.text.TextField;
var timestamp:int = 0;
var txt:TextField;
init();
function init():void
{
txt = new TextField();
txt.width = stage.stageWidth;
txt.height = stage.stageHeight;
addChild(txt);
}
function grabMessages():void
{
var ldr:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("getMessages.php");
var vars:URLVariables = new URLVariables();
vars.timestamp = timestamp;
req.data = vars;
ldr.addEventListener(Event.COMPLETE, updateMessages);
ldr.load(req);
}
function updateMessages($e:Event):void
{
var messages:XML = new XML($e.currentTarget.data);
for(var i:int; i<messages.length(); i++) txt.appendText("\n"+messages[i].value());
timestamp = new Date().getTime();
setTimeout(grabMessages, 3000);
}