vendredi 19 février 2016

From AMFPHP 1.9 to 2.x with Flash and MySql

There is not much information on how to update your communication between Flash > AMFPHP 2.x and AMFPHP > MySql when upgrading from AMFPHP 1.9 to 2.x. This worked for me.

Old Flash actionscript 3 (AMFPHP 1.9):

var arrayCompleet:Array = new Array();    
var gw:NetConnection = new NetConnection();
gw.connect("amfphp/gateway.php");
var res:Responder=new Responder(onResult,onFault);
gw.call("findBeads.findThem", res);  //class.function

function onResult(responds:Object):void {
    // parse the ArrayCollection form the db
    arrayCompleet=responds.serverInfo.initialData;
   // main arrays aanmaken
   for (var i:uint=0; i<arrayCompleet.length; i++) {
        arrayID[i]=arrayCompleet[i][0];
        arrayName[i]=arrayCompleet[i][1];   
   }
}

function onFault(responds:Object):void {
    //error
}

Old PHP code for findBeads.php (AMFPHP 1.9):

 <?php

 class findBeads
{   
    public function __construct()
    {
      mysql_connect("host", "db-name", "password"); 
      mysql_select_db("db-name")or die( "Probleem:".mysql_error()); 
    }

/**
 * Loads all beads from database
 * @returns all beads plus their info
 */
    function findThem()
    {
      return mysql_query("SELECT * FROM beads ORDER BY personID DESC");
    }
}

?>

New Flash actionscript 3 (AMFPHP 2.x):

var arrayCompleet:Array = new Array();    
var gw:NetConnection = new NetConnection();
gw.connect("/Amfphp/"); //new
var res:Responder=new Responder(onResult,onFault);
gw.call("findBeads.findThem", res);  //class.function

function onResult(responds:Object):void {
    // parse the ArrayCollection form the db
   // main arrays aanmaken
   for (var i:uint=0; i<responds.length; i++) { //new
        arrayID[i]=responds[i].id; //new add row name from mysql table
        arrayName[i]=responds[i].name; //new  add row name from mysql table
   }
}

function onFault(responds:Object):void {
    //error
}

New PHP code for findBeads.php (AMFPHP 2.x):

<?php

class findBeads
{   
    public static function getConnection() {

    return new PDO('mysql:host=host;dbname=db-name',
                    'username',
                    'password',
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
    );
}

/**
 * Loads all beads from database
 * @returns all beads plus their info
 */
    function findThem()
    {
        $pdo = findBeads::getConnection();
        $tsql = "SELECT * FROM beads ORDER BY personID DESC";
        $stmt = $pdo->prepare($tsql);
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
}

?>

Hope this helps.

Aucun commentaire:

Enregistrer un commentaire