mmm..pi..3.14..
06-23-2007, 11:29 PM
Long time, never seen...
Been working on this component off and on for over a year now, and I've used it on about 25 sites, so I thought it would be nice to let other people use it.
Basically it creates a link between Flash and MySQL using PHP (works with ASP.NET script too, but that is not ready for public release, so for now that stays with me). All that is needed is a working MySQL database and the mysql module for PHP must be installed. If you can connect to MySQL with PHP already, you should not need to bother checking anything.
Most of the common functions used in MySQL can be used in Flash with this component, below is the complete list:
mysql_connect()
mysql_select_db()
mysql_query()
mysql_fetch_assoc()
mysql_fetch_array()
mysql_fetch_row()
mysql_data_seek()
mysql_num_rows()
mysql_insert_id()
See the php documentation for each of those functions for parameter usage. Some parameters are not available in the flash versions of these functions, such as the "$client_flags" parameter in mysql_connect() The reason for that is either because I could not find a way to integrate that into the component, or I did not feel it was necessary (take your pick :p)
Here is an example of usage in flash (works as long as the MySQL Connector component is on the stage, doesn't need an instance name):
$Connection = mysql_connect("localhost", "username", "password");
trace($Connection); // Outputs: "Resource id #1"
// Both of the following methods work, since by default it uses
// the last open connection. You can give it the resource to select the db
// on, or omit it, in which case it will select the db for the last open connection
mysql_select_db("my_db", $Connection);
mysql_select_db("my_db");
// Once again, you can specify the resource to run the query on or not,
// it defaults to the last open connection
$MyQuery = mysql_query("SELECT * FROM my_table", $Connection);
$MyQuery = mysql_query("SELECT * FROM my_table");
trace($MyQuery); // Outputs: Resource id #2
// I have added the onResult function to the component, because unlike
// PHP, flash executes all script, without waiting for each line of code
// to finish executing (PHP/MySQL users should know what I'm talking about)
$MyQuery.onResult = function(OK){
if(OK){
trace(this);
// OR
trace($MyQuery);
// the above script outputs the query results exactly the same as it
// does when you run queries in the MySQL command prompt
// You can replace "this" with $MyQuery, doesn't matter which though
while($Row = mysql_fetch_assoc(this)){
trace($Row['column_name']);
}
// Could also do the following
// MYSQL_BOTH, MYSQL_ASSOC, and MYSQL_NUM are global variables
// stored in the component
while($Row = mysql_fetch_array(this, MYSQL_BOTH)){
trace($Row['column_1_name']);
trace($Row[0]);
// Both of the above output the same thing, since we
// used MYSQL_BOTH. If you used mysql_fetch_row(), you could
// also specify $Row[0], but not $Row['column_1_name']
}
}else{
// Something wrong happened, flash could not communicate with the db
}
}
// Connections close automatically, so there is no need for mysql_close()
The component will not allow simultaneous queries to run on the same connection, however I have built in a "Pending" class which lets you run multiple queries without screwing up something. The queries run in the order you have them in your script. The first query runs, broadcasts the "onResult" message, then runs the next query in line (if there is one), and so on... The reason for not allowing multiple queries to run simultaneously is because some queries run faster than others. If you need the script that runs after Query 1 gets a result to run before you execute Query 2, then it is good thing only 1 can run at a time. If multiple queries could run at once, Query 2 might get a result before Query 1, therefore causing problems.
You can run simultaneous queries if you would like, but they need to made on different resources. An example would look like this:
$Connection1 = mysql_connect("localhost", "username", "password");
$Connection2 = mysql_connect("localhost", "username", "password", true); // force a new connection, since we used the same credentials (see http://fr3.php.net/manual/en/function.mysql-connect.php, under the new_link parameter)
trace($Connection1); // Outputs: Resource id #1
trace($Connection2); // Outputs: Resource id #2
$Query1 = mysql_query("SELECT * FROM table_1", $Connection1);
$Query2 = mysql_query("SELECT * FROM table_2", $Connection2);
// Both queries run at the same time because they were made on different connections
$Query1.onResult = function(){
trace(this);
}
$Query2.onResult = function(){
trace(this);
}
In that script you might get the results from Query 2 back first, just depends on the query really.
The component itself has 3 parameters, Debugger, HTML Text, and Connector URL
Debugger
If turned on, query syntax errors and messages about not being able to connect to the connector url will output. The debugger works in flash and in web pages. If in flash, it will display the errors in the trace window. If in a web site, it will display the errors in a javascript alert window. Just makes it a bit more easy to debug.
HTML Text
If set to true, special html characters are converted to their html entities (i.e. - & becomes &, Æ becomes Æ, etc...)
If set to false, html entities are turned into their html text character (" becomes ", © becomes ©, etc...)
MySQL Connector URL
Pretty simple, the url to the PHP file.
Below is the script to "color" the mysql related function/variables. Copy to your AsColorSyntax.xml file, inside the <colorsyntax> element.
<keyword text="mysql_connect"/>
<keyword text="mysql_select_db"/>
<keyword text="mysql_query"/>
<keyword text="mysql_fetch_assoc"/>
<keyword text="mysql_fetch_array"/>
<keyword text="mysql_fetch_row"/>
<keyword text="mysql_num_rows"/>
<keyword text="mysql_insert_id"/>
<keyword text="mysql_data_seek"/>
<keyword text="MYSQL_BOTH"/>
<keyword text="MYSQL_NUM"/>
<keyword text="MYSQL_ASSOC"/>
Installation:
Copy the MySQLConnector.swc file to C:\Documents and Settings\<Username>\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Components\Data directory. If the "Data" directory does not exist, create it.
MySQL.php needs to go on a server capable of supporting PHP and MySQL. You can change the file name if you would like, just be sure and give the component the correct MySQL Connector URL.
The folder may vary depending on your computer and what version Flash you have. It has been developed in Flash 8, tested in Flash 7 and Flash 8. I do not know about other versions, so don't be surprised if it doesn't work in versions below Flash 7.
Enjoy :)
Been working on this component off and on for over a year now, and I've used it on about 25 sites, so I thought it would be nice to let other people use it.
Basically it creates a link between Flash and MySQL using PHP (works with ASP.NET script too, but that is not ready for public release, so for now that stays with me). All that is needed is a working MySQL database and the mysql module for PHP must be installed. If you can connect to MySQL with PHP already, you should not need to bother checking anything.
Most of the common functions used in MySQL can be used in Flash with this component, below is the complete list:
mysql_connect()
mysql_select_db()
mysql_query()
mysql_fetch_assoc()
mysql_fetch_array()
mysql_fetch_row()
mysql_data_seek()
mysql_num_rows()
mysql_insert_id()
See the php documentation for each of those functions for parameter usage. Some parameters are not available in the flash versions of these functions, such as the "$client_flags" parameter in mysql_connect() The reason for that is either because I could not find a way to integrate that into the component, or I did not feel it was necessary (take your pick :p)
Here is an example of usage in flash (works as long as the MySQL Connector component is on the stage, doesn't need an instance name):
$Connection = mysql_connect("localhost", "username", "password");
trace($Connection); // Outputs: "Resource id #1"
// Both of the following methods work, since by default it uses
// the last open connection. You can give it the resource to select the db
// on, or omit it, in which case it will select the db for the last open connection
mysql_select_db("my_db", $Connection);
mysql_select_db("my_db");
// Once again, you can specify the resource to run the query on or not,
// it defaults to the last open connection
$MyQuery = mysql_query("SELECT * FROM my_table", $Connection);
$MyQuery = mysql_query("SELECT * FROM my_table");
trace($MyQuery); // Outputs: Resource id #2
// I have added the onResult function to the component, because unlike
// PHP, flash executes all script, without waiting for each line of code
// to finish executing (PHP/MySQL users should know what I'm talking about)
$MyQuery.onResult = function(OK){
if(OK){
trace(this);
// OR
trace($MyQuery);
// the above script outputs the query results exactly the same as it
// does when you run queries in the MySQL command prompt
// You can replace "this" with $MyQuery, doesn't matter which though
while($Row = mysql_fetch_assoc(this)){
trace($Row['column_name']);
}
// Could also do the following
// MYSQL_BOTH, MYSQL_ASSOC, and MYSQL_NUM are global variables
// stored in the component
while($Row = mysql_fetch_array(this, MYSQL_BOTH)){
trace($Row['column_1_name']);
trace($Row[0]);
// Both of the above output the same thing, since we
// used MYSQL_BOTH. If you used mysql_fetch_row(), you could
// also specify $Row[0], but not $Row['column_1_name']
}
}else{
// Something wrong happened, flash could not communicate with the db
}
}
// Connections close automatically, so there is no need for mysql_close()
The component will not allow simultaneous queries to run on the same connection, however I have built in a "Pending" class which lets you run multiple queries without screwing up something. The queries run in the order you have them in your script. The first query runs, broadcasts the "onResult" message, then runs the next query in line (if there is one), and so on... The reason for not allowing multiple queries to run simultaneously is because some queries run faster than others. If you need the script that runs after Query 1 gets a result to run before you execute Query 2, then it is good thing only 1 can run at a time. If multiple queries could run at once, Query 2 might get a result before Query 1, therefore causing problems.
You can run simultaneous queries if you would like, but they need to made on different resources. An example would look like this:
$Connection1 = mysql_connect("localhost", "username", "password");
$Connection2 = mysql_connect("localhost", "username", "password", true); // force a new connection, since we used the same credentials (see http://fr3.php.net/manual/en/function.mysql-connect.php, under the new_link parameter)
trace($Connection1); // Outputs: Resource id #1
trace($Connection2); // Outputs: Resource id #2
$Query1 = mysql_query("SELECT * FROM table_1", $Connection1);
$Query2 = mysql_query("SELECT * FROM table_2", $Connection2);
// Both queries run at the same time because they were made on different connections
$Query1.onResult = function(){
trace(this);
}
$Query2.onResult = function(){
trace(this);
}
In that script you might get the results from Query 2 back first, just depends on the query really.
The component itself has 3 parameters, Debugger, HTML Text, and Connector URL
Debugger
If turned on, query syntax errors and messages about not being able to connect to the connector url will output. The debugger works in flash and in web pages. If in flash, it will display the errors in the trace window. If in a web site, it will display the errors in a javascript alert window. Just makes it a bit more easy to debug.
HTML Text
If set to true, special html characters are converted to their html entities (i.e. - & becomes &, Æ becomes Æ, etc...)
If set to false, html entities are turned into their html text character (" becomes ", © becomes ©, etc...)
MySQL Connector URL
Pretty simple, the url to the PHP file.
Below is the script to "color" the mysql related function/variables. Copy to your AsColorSyntax.xml file, inside the <colorsyntax> element.
<keyword text="mysql_connect"/>
<keyword text="mysql_select_db"/>
<keyword text="mysql_query"/>
<keyword text="mysql_fetch_assoc"/>
<keyword text="mysql_fetch_array"/>
<keyword text="mysql_fetch_row"/>
<keyword text="mysql_num_rows"/>
<keyword text="mysql_insert_id"/>
<keyword text="mysql_data_seek"/>
<keyword text="MYSQL_BOTH"/>
<keyword text="MYSQL_NUM"/>
<keyword text="MYSQL_ASSOC"/>
Installation:
Copy the MySQLConnector.swc file to C:\Documents and Settings\<Username>\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Components\Data directory. If the "Data" directory does not exist, create it.
MySQL.php needs to go on a server capable of supporting PHP and MySQL. You can change the file name if you would like, just be sure and give the component the correct MySQL Connector URL.
The folder may vary depending on your computer and what version Flash you have. It has been developed in Flash 8, tested in Flash 7 and Flash 8. I do not know about other versions, so don't be surprised if it doesn't work in versions below Flash 7.
Enjoy :)