Tuesday, March 29, 2011

Flex 4, PHP, MySQL

In this blog posting I will show you how to connect Flex to PHP an load MySQL data without writing single line of code.

Couple things we need to install before we start.

1. Flash Builder - https://www.adobe.com/cfusion/tdrc/index.cfm?product=flash_builder
2. MySQL Database - http://dev.mysql.com/downloads/mysql/5.1.html
3. SQL Youg - MySQL Admin - http://www.webyog.com/en/downloads.php or you can use MySQL Administrator - http://dev.mysql.com/doc/administrator/en/

Lets Start!

Step 1:

Create new database:


  • Connect to your database server via SQLyog
  • Right click on root@local and create new database
  • Name it - test





    Step 2:


    Create table:
  • Right Click on test->Tables
  • Add Columns to table: id  - int, name, email, clientIP as varchar(40), make id - primary key

    or just run the following query in Query Window

    CREATE TABLE `subscribers` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(50) DEFAULT NULL,
      `email` varchar(50) DEFAULT NULL,
      `clientIP` varchar(30) DEFAULT NULL,
      PRIMARY KEY (`id`)
    )

    Step 3:

    Add some test data on the table, my data looks like this:


    Step 4:

    • Open Flash Builder 4 IDE
    • Create new Flex project
    • Select Application server type - PHP
    • I named my project - Flex_PHP_MySQL
    • Switch from Source to Design mode




    Step 5:

    Add label to the application by draging the label from Components panel.
    Add text to label - "Total Count:"
    Add new label next to it - this is our table records counter

    Step 6:

    Right click on second label and and click on Bind to Data...

    Question will popup


    No services are defined for this project.
    Do you want to connect to one now?

    Click Yes

    Select PHP as service type and click Next

    Click on click here to generate a sample

    Use correct user name, password, Host name and database name to connect to your MySQL database.

    After Connecting to databse in the section below called table, you will be able to see our new created table - subscribers

    Click "OK", the Flash builder will generate php file for you.

    Step 7:

    This is the last step, all we are going to do is connect our label to our service.

    right click on our label in the Flash Builder Design windows

    select Service: SubscribersService
    seelct Operation: count() : int

    Click ok and run the projects.

    You should be able to see the Row Count : ....

    Download Sample Flex 4 project





1 comment: