How to Connect to a Database With AS3 Tutorial
ActionsScript3(AS3) -- a programming language that is a part of the Adobe Flash, the Adobe AIR and the Adobe Flash Builder platforms -- is used not only for programming animations, but also for file and data transfer tasks. One such task is connecting to, retrieving and sending information to a database. With AS3 file and data transfer classes, methods and properties, you can connect your Flash program to a number of different databases. The database -- whether it is a MySQL, a Microsoft Access, or an AIR-generated local SQL database -- requires a different approach to make the connection.
Things You'll Need
- Web hosting provider with PHP, MySQL, ColdFusion and Microsoft Access support.
Instructions
-
Connecting to MySQL Databases
-
1
Create one AS3 file in Adobe Flash Builder or an AIR Flash file in the Flash Professional software package.
-
2
Type the "URLRequest" method in the AS3 file and specify its argument to be the file location of the PHP connection file on the Web server. Refer to www.flash-db.com (see Resources) for the exact syntax for requesting access to the PHP file with the AS3 URLRequest method.
-
-
3
Create a text file to place the PHP connection code that will connect the AS3 file to the database to a MySQL database.
-
4
Use the "mySQLconnect" function in the PHP connection file for connecting to the MySQL database with AS3. Specify the host server name, your user name and your password for the MySQL database you are accessing as the arguments in the PHP "mysql_connect" function. Use the PHP "mysql_select_db" function to select the specific MySQL database with the PHP variable you used to store the "mysql_connect" function. Refer to www.flash-db.com (see Resources) for the exact syntax to use for the mySQL connection function.
Connection to Microsoft Access Databases
-
5
Create one AS3 file in Adobe Flash Builder or an AIR Flash file in the Flash Professional software package. Type the "URLRequest" method in the AS3 file, and specify its argument to be the file location of the ColdFusion connection file on the Web server. Refer to www.flash-db.com (see Resources) for the exact syntax to use with the URLRequest method with a ColdFusion connection file.
-
6
Set the ColdFusion database connection information and the type of database to connect to with the ColdFusion Administrator. Click the "Add New Data" option, and then enter Microsoft Access for the driver type. Click "Add." Enter a datasource name to access the database connection information, the database name you want to connect to, the name of the server the database is on and the username and password required to gain access to the database. Refer to the www.quackit.com (see Resources) for more specifics on using the ColdFusion Administrator.
-
7
Create a text file with a text editor to place the ColdFusion connection code that will connect the AS3 file to the Microsoft Access database. Type the ColdFusion "cfquery,datasource" command in the ColdFusion in this file with the datasource name you specified in the ColdFusion administrator.
Connecting to SQL Databases
-
8
Create one AS3 file in Adobe Flash Builder or an AIR Flash file in the Flash Professional software package.
-
9
Use a SQLConnection object with either the open or openAsync database connection method in the AS3 file to connect to a local SQL database.
-
10
Use the name of the local SQL database, stored in a File object, with the "resolvePath" method as the argument for the SQLConnection object. Refer to the www.livedocs.adobe.com website (see Resources) for the actual code and required syntax for connecting to a local SQL database.
-
1
Tips & Warnings
Refer to the AIR, ColdFusion, FlashBuilder(Flex) and ActionScript developer guides and the PHP Web pages listed in the "Resources" section below to obtain the exact specifics on the syntax of the code to use to make connections to other types of databases as well as for specifying other database connection options.
References
- Adobe Systems; ActionScript 3.0 Developers Guide; Creating and Modifying a Database
- ActionScript® 3.0 Adobe Systems; Reference for the Adobe® Flash® Platform; SQLMode; April 2011
- ActionScript® 3.0 Adobe Systems; Reference for the Adobe® Flash® Platform; SQLStatement; April 2011
- ActionScript® 3.0 Adobe Systems; Reference for the Adobe® Flash® Platform; SQLConnection; April 2011
- quackit; ColdFusion Database Queries (cfquery)
- THE FLASH-DB; Saving dynamic data from Flash using AS3
Resources
- w3Schools; PHP mysql_connect() Function
- Adobe; Developing AIR applications with Flex; Creating and modifying a database
- Adobe; ColdFusion Developer's Guide; Chapter 22: Introduction to Databases and SQL
- quackit; ColdFusion Datasource
- w3schools; PHP mysql_select_db() Function
- THE FLASH-DB; Saving dynamic data from Flash using AS3; 4. Saving data to databases (php-asp-cfm code)
- Photo Credit Thinkstock/Comstock/Getty Images