Using MySQL with PHPObject
Author
Vin (http://www.nostamprequired.com)
Overview
In this tutorial, we are going to use PHPObject to obtain data from a MySQL database and display the information 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 a 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
The following shows the actionscript you should enter in the actions panel: |
// import the library
#include "PHPObject.as"
// set up gateway url and key
_global.defaultGatewayUrl = "http://mydomain.com/services/Gateway.php";
_global.defaultGatewayKey = "secret";
// creates the object
myFoo = new PHPObject("cards");
// sets 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 remote method passing in the cardid as parameter
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 class (cards.php): |
< ?php
class cards
{
function getcard($getcard)
{
$db_name = "yourdbname";
$connection = @mysql_connect("localhost", "username", "password") or die("Could 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.
|
|
About PHPObject |
PHPObject is an opensource alternative to Flash Remoting for PHP developers. With PHPObject, you can call a method of a PHP class/library on your web server as if the class/library was defined in Flash itself. It takes care of your client-server connections and makes passing of variables (properties) between Flash MX and PHP easy, and thereby providing a convenient way to connect rich media clients with data and business logic residing on your server.
PHPObject is developed by GhostWire Studios and released under the GNU LGPL. |
|
|
>> Documentation <<
|
|