Introducing
Flash send and expects data from and to the outside world using unicode UTF-8 (see technote).
For a brief introduction you can also read this article.
In this article we will see how Flash can have different results both reading and writing data into a MySQL database using different options in the amfphp gateway.
1. What is Unicode?
Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. Incorporating Unicode into client-server or multi-tiered applications and websites offers significant cost savings over the use of legacy character sets. Unicode enables a single software product or a single website to be targeted across multiple platforms, languages and countries without re-engineering. It allows data to be transported through many different systems without corruption. (from unicode.org)
2. MySQL character set and collations
Latest releases of the popular database introduced character set and collation definition.
When you create a database, a table you can specify both a character set and a collation.
CREATE TABLE `table_strings` ( `id` int(11) NOT NULL auto_increment, `f_string` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3. Flash movie
Now create a simple flash movie just for reading and writing strings into our table.
It will looks like this image:
What we have in the flash movie is:
- An input textfield "user_text", where you can write strings which will be passed to amfphp
- A "send_btn" to call the remote method "InsertString"
- A Datagrid for display the MySQL records
- A "refresh_btn" for calling remote method "GetStrings" (for populating DataGrid)
/** remoting classes **/
import mx.remoting.Service
import mx.remoting.PendingCall
import mx.rpc.RelayResponder
import mx.rpc.ResultEvent
import mx.rpc.FaultEvent
/** alert window **/
import mx.controls.Alert
// create the service
var service:Service = new Service("http://localhost:8080/amfphp/gateway.php", null, "tutorials.tutorial1");
// Calling "SendData" remote method
// passing the string written in the input
// text field
function sendData(evt:Object)
{
var text:String = evt.target._parent.user_text.text
var pc:PendingCall = evt.target._parent.service.InsertString(text)
pc.responder = new RelayResponder(evt.target._parent, "onInsertResult", "onFault")
}
// refresh the datagrid
// empty the grid and calling remote method "GetStrings"
function refreshData(evt:Object)
{
my_grid.removeAll();
var pc:PendingCall = evt.target._parent.service.GetStrings()
pc.responder = new RelayResponder(evt.target._parent, "onRefreshResult", "onFault")
}
// General fault responder
function onFault(f:FaultEvent)
{
Alert.show(f.fault.faultstring, "Error", Alert.OK, this)
}
// Insert operation was ok
function onInsertResult(r:ResultEvent)
{
Alert.show(r.result, "Result", Alert.OK, this)
user_text.text = ""
}
// adding Datagrid dataProvider
// passing directly the result arrived from
// amfphp
function onRefreshResult(r:ResultEvent)
{
my_grid.dataProvider = r.result
}
/** add listeners to the buttons on the stage **/
this.send_btn.addEventListener("click", sendData)
this.refresh_btn.addEventListener("click", refreshData)
