Recently I started on a project that was going to use a MySQL database. Unfortunately it wasn't going to be an AIR app or I could have followed Seth's tutorial for using SQLite with AIR. No, this was going to be fairly straightforward Flex only. So how to get started with Flex and MySQL?
To get a very basic example up and running is just a handful of quick steps. First of all, you'll need some sort of mediator between Flex and MySQL, PHP handles this part. Get acquainted with AMFPHP to make it easier on yourself. AMFPHP makes it easy to set up your services to talk to PHP classes who will in turn handle your MySQL calls.
After getting AMFPHP installed you'll need to start writing your services in Flex. The service is going to communicate with AMFPHP using a RemoteObject.
The destination specified in the RemoteObject instantiation is always "amfphp." The endpoint is going to point to wherever your gateway.php file is located. In this case I am running it locally, otherwise it would point to http://yourserver.com/amfphp/gateway.php. The source refers to the name of the PHP class that holds your MySQL statements. I tend to make one remote object per PHP class to keep the services separated neatly.
Next you need to set up your PHP class with methods you want to call from Flex. This example is going to use a basic SELECT statement to select all rows in a table, it looks like this:
Once your PHP class has all the MySQL calls you need to make you can call them from your service.
Whatever method you are calling on your remote object is the name of a method in your PHP class, in this case I am calling selectAllRows() in my PHP class and am being returned an ArrayCollection of the rows that have been selected from my db.
And that's how easy it is to start communicating with your MySQL database from Flex! From here you can use that data just like you would any other collection. Of course this example only returns generic objects from the database, but they can easily be mapped to value objects if you specify. Stay tuned for a more extensive example that will show how to do this!
3 Comments
nicely done would love to see more on the mysql aspect of flex.
I can’t find any mention of the installation procedure for amfphp for flex… Which directories need to be copied? and to where?
David, check out the resources on the amfphp website for how to install:
http://www.amfphp.org/docs/installingamfphp.html