Home
HomeFlexible, Lightweight and Truly Skinnable Flash ComponentsPHPObject and other Flash ExtensionsForumsBlog JournalLinks
Buy Flash Components
  PHPObject - Opensource PHP Flash Remoting

redhand Using PHPObject (MySQL)

Getting SQL results, stored in an array
The following example shows how you can get the results returned by a MySQL query, stored in an array:


Example 1:
// ** PHP **
// a function in the PHP class that does a simple query and returns the results to Flash

function getRecords($dbName, $tableName)
{
    $db = mysql_connect("localhost", "username", "password");
    mysql_select_db($dbName, $db);
    // here's the query
    $result = mysql_query("SELECT * FROM $tableName ");
    // here's how we store the results in an array
    $rs = array();
    while ($row = mysql_fetch_row($result))
    {
        array_push($rs, $row);
    }
    return $rs;
}

// ** Actionscript **
// sets up the responder
myFoo.getRecords_onResult = function(result)
{
     var j = result.length;
     for (var i=0; i<j; i++)
     {
        // displays each row in the output panel
         trace("---Record "+i+"---")
         trace(result[i]);
     }
}

 

Getting SQL results, stored as associative arrays
If you wish to get associative arrays as your results, so that Flash will get an array of objects (instead of an array of arrays), then you should use mysql_fetch_object.


Example 2:
// a function in the PHP class that does a simple query and returns the results to Flash
function getRecords($dbName, $tableName)
{
    $db = mysql_connect("localhost", "username", "password");
    mysql_select_db($dbName, $db);
    // here's the query
    $result = mysql_query("SELECT * FROM $tableName ");
    // here's how we store the results in an array
    $rs = array();
    while ($row = mysql_fetch_object($result))
    {
        array_push($rs, $row);
    }
    return $rs;
}

// ** Actionscript **
// sets up the responder

myFoo.getRecords_onResult = function(result)
{
     var j = result.length;
     for (var i=0; i<j; i++)
     {
        // displays records in the output panel
          trace("---Record "+i+"---")
          for (var a in result[i])
          {
               trace("key:"+a+"=>value:"+result[i][a]);
          }
     }
}
Take note however, that this method of returning SQL results can greatly bloat your message size since you are now transferring the key names together with the values for each record. It will be more efficient to use simple arrays (Example 1 above) and then parsing them into associative arrays once you get them on the client side.
Example 3:
// a function in the PHP class that does a simple query and returns the results to Flash
function getRecords($dbName, $tableName)
{
    $db = mysql_connect("localhost", "username", "password");
    mysql_select_db($dbName, $db);
    // here's the query
    $q_result = mysql_query("SELECT * FROM $tableName ");
    // here's how we store the results in an array
    $rs = array();
    while ($row = mysql_fetch_row($q_result))
    {
        array_push($rs, $row);
    }
    // we also need information on the field names
    $keys = array();
    for ($i=0; $i < mysql_num_fields ($q_result); $i++)
    {
        array_push($keys, mysql_field_name ($q_result, $i) );
    }
    // we return an associative array with two properties: 'keys' and 'values'
    $result = array();
    $result['keys'] = $keys;
    $result['values'] = $rs;
    return $result;
}

// ** Actionscript **
// sets up the responder

myFoo.getRecords_onResult = function(result)
{
     // prepare the results
     var r = [];
     var b = result['keys'].length;
     var j = result['values'].length;
     for (var i=0; i<j; i++)
     {
          r[i] = {};
          for (var a=0; a<b; a++)
          {
               r[i][result['keys'][a]] = result['values'][i][a];
          }
     }
     result = r;
     // we now have the results like in Example 2 above
     var j = result.length;
     for (var i=0; i<j; i++)
     {
        // displays records in the output panel
          trace("---Record "+i+"---")
          for (var a in result[i])
          {
               trace("key:"+a+"=>value:"+result[i][a]);
          }
     }
}

 

Executing SQL statements from Flash
For security reasons, It is strongly recommended that you keep all SQL statements on the server-side. There is no reason why you should allow Flash to execute SQL queries directly. But if for some strange reason you wish to do so, here is an example of a PHP function that accepts a SQL statement as its parameter, and executes that SQL:


Example 4:
// a function in the PHP class that executes a supplied SQL query and returns the results to Flash
function doSQL($sql, $dbName)
{
    $db = mysql_connect("localhost", "username", "password");
    mysql_select_db($dbName, $db);
    // here's the query
    $result = mysql_query($sql);
    // here's how we return the results in an array
    $rs = array();
    while ($row = mysql_fetch_row($result))
    {
        array_push($rs, $row);
    }
    return $rs;
}

// ** Actionscript **
// sets up the responder
myFoo.getRecords_onResult = function(result)
{
     var j = result.length;
     for (var i=0; i<j; i++)
     {
        // displays each row in the output panel
         trace("---Record "+i+"---")
         trace(result[i]);
     }
}

Once again, please be reminded that for security reasons, you *should not* allow SQL statements to be executed directly from Flash.


>> Documentation <<

 
Contact UsSite Map
Copyright © 2003-2006 GhostWire Studios