[English version available further below] Voici une démonstration de connexion de Flash avec une base de données MySQL. Cette démonstration montre la lecture et l'écriture dans une base de données (MySQL) à partir de Flash en utilisant la technique des updataPacket. Un 'updatePacket' permet d'écrire dans la base de données les modifications (et seulement les modifications) qui ont été effectuées par l'utilisateur sur le modèle de données. (changement de valeur, effacement d'un enregistrement, création d'un enregistrement). Cet exemple permet aussi la sauvegarde et la restauration des données sur le poste client avec les SharedObject. Pre-requis =========== - serveur IIS ou Apache - base de données MySQL - phpMyAdmin version 2.5.4 : http://www.phpmyadmin.net - PHP version 5.0.0 beta2 ou supérieure : http://www.php.net/downloads.php Liste des fichiers ================== connectdb.php : paramétrage de la connexion à la base de données MySQL getdata.php : lecture de la BD et génération du fichier XML pour les données savedata.php : écriture dans la BD à partir d'un fichier XML updatePacket et génération du fichier XML de résultat MainForm.as : les actions en ActionScript initdb.php : fichier pour l'initialisation de la base MySQL contact.sql Installation ============ Vous devez configurer Apache (ou IIS) pour pourvoir utiliser PHP et mySQL. Installation ------------ - Dézipper le tout dans un répertoire de Apache (ou IIS) => vous devez avoir accès au répertoire suivant : contactphp Création de la base et des tables --------------------------------- - créer une base avec phpMyAdmin - modifier le fichier initdb.php en remplacant myhostname myusername mypassword mydatabase avec vos identifiants. - lancer le fichier initdb.php pour créer la table contact avec un enregistrement http://localhost/contactphp/initdb.php - vérifier que la table "contact" a bien été créée avec phpMyAdmin - modifier le fichier connectdb.php en remplacant myhostname myusername mypassword mydatabase avec vos identifiants. - lancer l'application Lancement de l'application ========================== Lancer Microsoft Internet Explorer (ou un autre navigateur) et entrer comme URL: http://localhost/contactphp/contact.html Vous pouver créer de nouveaux contacts, modifier un contact et/ou effacer des contacts. Techniques pour le debug ======================= Le debug d'une application avec connexion avec une base de données sur un serveur est un sport de haut-niveau ;-) Debug dans l'environnement Flash -------------------------------- - pour réaliser du debug dans Flash, il est conseillé de modifier les URL des XMLConnector. XMLConnector contact_con : - mettre comme URL : http://localhost/contactphp/getdata.php (au lieu de getdata.php) XMLConnector save_con - mettre comme URL : http://localhost/contactphp/savedata.php Vous pouvez tester votre application dans l'environnement Flash. Debug de la lecture de la BD et de la génération dufichier XML -------------------------------------------------------------- Il suffit de lancer http://localhost/contactphp/getdata.php Vous pouvez visualiser le fichier XML généré avec Internet Explorer Test de l'écriture dans la BD à partir du fichier XML updatePacket ------------------------------------------------------------------ Il faut tester en deux étapes : 1- Modifier le fichier savedata.php en modifiant la ligne suivante : $saveUpdatePacket = true; Lancer l'application Flash avec une écriture dans la BD. Lorque l'application Flash écrit dans la BD, le fichier XML généré par Flash est sauvé localement dans le fichier updatePacket.xml 2- Tester l'écriture dans la BD par : http://localhost/contactphp/savedata.php?debug=1 Vous lancer l'écriture dans la BD avec le fichier local updatePacket.xml Si des erreurs PHP se produisent, il n'y a plus qu'à les corriger. Bons tests... PhilFlash http://philflash.inway.fr philflash@inway.fr ===================================================================== ===================================================================== Here is a demonstration of how Flash can be connected to a database with MySQL. The example shows how to read and write in a database (MySQL) from Flash using the updatePacket technique. An 'updatePacket' can write modifications - and only modifications - to a database which were done by the user on the data model (changing a value, deleting a record, creating a record). This example illustrates also how data can be saved and restored on the client side with SharedObject. Requirements =========== - Server IIS or Apache. - MySQL database. - phpMyAdmin version 2.5.4 or higher: http://www.phpmyadmin.net. - PHP version 5.0.0 beta2 or higher: http://www.php.net/downloads.php. Files ================== connectdb.php : connection parameters to the MySQL database. getdata.php : reading of the DB and generation of an XML file for the data. savedata.php : writing to the DB (with decoding of an updatePacket XML file) and generation of an XML file for the result. MainForm.as : actions in ActionScript. initdb.php : initialization of the MySQL database. contact.sql Installation ============ You must configure Apache (or IIS) to use PHP version 5 and MySQL. Installation ------------ - Unzip the whole package into a separate directory. => you must have access to the following directory: contactphp. Creation of the database, the table structure and the data ----------------------------------------------------------- - Create a database with phpMyAdmin. - Modify the file 'initdb.php' and replace the following variables to reflect your configuration: myhostname myusername mypassword mydatabase - Run the file 'initdb.php' to create the table structure and the data http://localhost/contactphp/initdb.php. - Check that the table "contact" was actually created with phpMyAdmin. - Edit the file 'connectdb.php' and replace myhostname myusername mypassword mydatabase with your own identifiers. - Run the application Running the application ======================= - Start a browser and enter the following URL: http://localhost/contactphp/contact.html - You can create, modify or remove a contact. Debugging techniques ==================== Debugging an application with a connection to a database server is top-level sports ;-). Debugging within the Flash environment -------------------------------------- - To make debugging from Flash easier, it's better to modify the XMLConnector URLs. XMLConnector contact_con: - Use the URL http://localhost/contactphp/getdata.php (instead of getdata.php). XMLConnector save_con: - Use the URL http://localhost/contactphp/savedata.php. Now you can test your application in the Flash environment. Debugging reading the DB and generating the XML file ---------------------------------------------------- All you need to do is running http://localhost/contactphp/getdata.php. You can display the XML file which is generated with a browser. Testing writing in the DB with an XML updatePacket file ------------------------------------------------------- It takes two steps: 1- Edit the file 'savedata.php' and set $saveUpdatePacket to true: $saveUpdatePacket = true; Run the Flash application which writes to the DB. When the Flash application writes to the DB, the XML file generated by Flash is saved locally in the file 'updatePacket.xml'. 2- Test writing to the DB: http://localhost/contactphp/savedata.php?debug=1 Start writing to the DB with the local file 'updatePacket.xml'. If you encounter an error, just correct it. Bons tests... PhilFlash http://philflash.inway.fr philflash@inway.fr