Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Integrating Flash and mySQL

Integrating Flash and mySQL

By Vin | Published 09/9/2005 | Beginner | Rating:
Vin
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. 

View all articles by Vin
Page 1 of 1
Written by: Vin | nostamprequired.com
Difficulty Level: Beginner
Requirements: Flash MX + PHP This tutorial makes use of the PHPObject code which can be found at: http://ghostwire.com/resources/phpobject/

Please download the PHPObject and try out the sample to better understand what's going on here.
This is just a basic tutorial on how to display data in a mySQL database in flash quickly and easily. You can develop this code further to create dynamic Flash movies that feed off a mySQL database. In this example I'm using an eCard project I'm working on. The code will connect to my database to get the card information (the to, from, and message) and display this data in the flash movie. Also note that you should have dynamic text fields named as each of the variables your want to display in your flash movie

The Flash Code:

#include "PHPObject.as"

tmp = new PHPObject();
tmp.setDefaultGatewayKey("secret"); // CHANGE THIS TO
//WHATEVER SECRET KEY YOU SET IN Gateway.php
tmp.setDefaultGatewayUrl("http://www.yourdomain.com/pathto/Gateway.php"); // CHANGE THIS
//TO WHERE YOU PLACE Gateway.php
delete tmp;

myFoo = new PHPObject("cards"); // declare a new object and link it to a remote PHP class


// set up responder
myFoo.getcard_onResult = function(result) {
       
        id.text = result[0];
        sid.text = result[1];
        s_name.text = result[2];
        s_email.text = result[3];
        r_name.text = result[4];
        r_email.text = result[5];
        message.text = result[6];
        card.text = result[7];
        uniqueid.text = result[8];
        beenread.text = result[9];
}

// invoke method
myFoo.getcard("rJ7WP605");

 

So what we're doing is setting up the Gateway to the PHP file first. Then we set up the code in Flash to get the array PHP will return to us and to put each part of the array into seperate input text boxes on in the flash movie. The invoke method starts the process. It calls the php file "cards.php" as specified in the PHPObject function. Now we have to write the PHP to get the database info and return it to flash.



The PHP Code (cards.php):

<?

class cards {
        function getcard($getcard) {
               
                $db_name = "yourdbname";
                $connection = @mysql_connect("localhost", "username", "password") or die("Cound not connect to database");
                mysql_select_db($db_name, $connection) or die("Count not select database");
                $query = "SELECT * FROM ecards WHERE sid LIKE '$getcard'";
                $q_result = mysql_query($query);
                return mysql_fetch_row($q_result);
               
        }
}

?>

This is just some basic PHP code to connect to a mySQL database and return the results. The getcard() function requires a parameter in this example. This parameter tells mySQL which record it wants to pull. In this case it's the id of the card. Once the SQL query has been run, we just send the row as an array and let flash parse it. You can manipulate the data however you want in the PHP, then just have Flash display it.
How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent

Verification:
Enter the security code shown below:
img


Add comment

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by Hendra - hen_squal at yahoo.com)
    Rating
    is this connection between php n flash with mysql? can you tell me how if I want connect flash with mysql. Only both , flash and mysql.If you know about connection, can you help me send to my email..thx u
     
  • Comment #2 (Posted by Keyur - gajjarkeyur at yahoo.com)
    Rating
    i would like to know more about on this article any one can help me . in Flash where to write this scrip means onLoad or in movie
     
  • Comment #3 (Posted by Mochamad Joko Adi W - joko_aditya at yahoo.com)
    Rating
    Hello...
    I 'am beginner programmer with flash and php...
    me want question, how connection flash with php + actionScript + databases mysql...?
    Thank's you very much
     
  • Comment #4 (Posted by eros - aren728 at yahoo.com)
    Rating
    can you tell me what version of flash and mysql are used here? thanks. please do send me and email asap.
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.