How to connect to the database using Flex:

Source: Internet
Author: User

How to connect to the database using Flex:

Flex applications are not directly connected to a database, so you need to use a certain type of service.
To support the use of data.

The first step to connect to the database is to generate the data that will be used in the flex program. In applications with PHP
Follow these steps:
1. Create a database (such as MySQL ).
2. Write a PHP script to connect to the MySQL database and generate data in XML format.
These steps are also suitable for data generated on other work platforms (such as ASP. NET and JSP ).
Connect external data sources
For XML format data generated by PHP, you can use the httpservice component to request data,
Like this:
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: httpservice
Id = "productsrequest"
Url = "http://www.somesite.com/products.php"/>
...
The httpservice component defines a request ID. You can use this ID to control the URL of the provided data or the binding between the server and the data.

Binding of external data to data-driven control
By binding data to data-driven control, you can process the results of httpservice (XML data) as follows:
<Mx: DataGrid x = "20" Y = "80" id = "productgrid" width = "400"
Dataprovider = "{productrequest. lastresult. Products. Items}">
<Mx: columns>
<Mx: datagridcolumn headertext = "name" datafield = "name"/>
<Mx: datagridcolumn headertext = "price" datafield = "price"/>
</MX: columns>
</MX: DataGrid>
The Data Binding syntax is displayed in the dataprovider attribute of Data Control (in curly brackets). It contains the httpservice Request ID, lastresult method, and the data structure of the XML file. In this example, the data structure of the XML data source looks like this:
<XML>
<Products>
<Item>
<Name> mobile phone </Name>
<Price> $199 </price>
</Item>
<Item>
<Name> Car Charger </Name>
<Price> $34 </price>
</Item>
...
By setting the datafield attribute, project data (name and price) is used as the data of each column in the data grid.

Load data at runtime
You can also load data when the flex program starts running, as shown later, sending a request to a specific URL in httpservice:
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "productsrequest. Send ()">
After you add the creationcomplete method to the application tag, the data is loaded at runtime and handed over to the data driver control (in this example, the data grid ).
You can also add an httpservice request to a control event rather than a tag in the program, as shown below:
<Mx: button x = "50" Y = "8" label = "Get Data" Click = "productsrequest. Send ();"/>
Flex provides many methods to connect, manage, format, and verify data. You can use remote program calls, data services, or other enterprise-level technologies to manipulate and manage data.

Address:Http://www.bhcode.net/article/20080923/2520.html

Flex applications are not directly connected to a database, so you need to use a certain type of service.
To support the use of data.

The first step to connect to the database is to generate the data that will be used in the flex program. In applications with PHP
Follow these steps:
1. Create a database (such as MySQL ).
2. Write a PHP script to connect to the MySQL database and generate data in XML format.
These steps are also suitable for data generated on other work platforms (such as ASP. NET and JSP ).
Connect external data sources
For XML format data generated by PHP, you can use the httpservice component to request data,
Like this:
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: httpservice
Id = "productsrequest"
Url = "http://www.somesite.com/products.php"/>
...
The httpservice component defines a request ID. You can use this ID to control the URL of the provided data or the binding between the server and the data.

Binding of external data to data-driven control
By binding data to data-driven control, you can process the results of httpservice (XML data) as follows:
<Mx: DataGrid x = "20" Y = "80" id = "productgrid" width = "400"
Dataprovider = "{productrequest. lastresult. Products. Items}">
<Mx: columns>
<Mx: datagridcolumn headertext = "name" datafield = "name"/>
<Mx: datagridcolumn headertext = "price" datafield = "price"/>
</MX: columns>
</MX: DataGrid>
The Data Binding syntax is displayed in the dataprovider attribute of Data Control (in curly brackets). It contains the httpservice Request ID, lastresult method, and the data structure of the XML file. In this example, the data structure of the XML data source looks like this:
<XML>
<Products>
<Item>
<Name> mobile phone </Name>
<Price> $199 </price>
</Item>
<Item>
<Name> Car Charger </Name>
<Price> $34 </price>
</Item>
...
By setting the datafield attribute, project data (name and price) is used as the data of each column in the data grid.

Load data at runtime
You can also load data when the flex program starts running, as shown later, sending a request to a specific URL in httpservice:
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "productsrequest. Send ()">
After you add the creationcomplete method to the application tag, the data is loaded at runtime and handed over to the data driver control (in this example, the data grid ).
You can also add an httpservice request to a control event rather than a tag in the program, as shown below:
<Mx: button x = "50" Y = "8" label = "Get Data" Click = "productsrequest. Send ();"/>
Flex provides many methods to connect, manage, format, and verify data. You can use remote program calls, data services, or other enterprise-level technologies to manipulate and manage data.

Address:Http://www.bhcode.net/article/20080923/2520.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.