Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


AMFPHP and MySQL character set

4. the gateway.php

The gateway.php file is the bridge between your flash .swf file and your service php class.
Within the gateway you can set different options such as the baseClassPath, the LooseMode, disableStandalonePlayer, disableTrace etc. but for this tutorial we will focus out attention on the setCharsetHandler method.

The method is:

setCharsetHandler(string mode, string phpCharset, string sqlCharset)
            

4.1 string mode

the first parameter used in setCharsetHandler accepts this values:
  • none: don't do anything
  • iconv: uses the iconv libray for reencoding
  • mbstring: uses the mbstring library for reencoding
  • recode: uses the recode library for reencoding
  • utf8_decode: uses the XML function utf8_decode and encode for reencoding (ISO-8859-1 only)

4.1.1 ICONV

This modules provides the best way for string conversion. If your php is compiled and configured with iconv support then iconv is the best choice. In order to know if your php support iconv just see your phpinfo and look for a section "iconv" like this:

phpinfo iconv

See the php manual about iconv here: http://php.net/iconv
GNU libiconv encodings table: http://www.gnu.org/software/libiconv/

4.1.2 MBSTRING

Multibyte String Functions. This module provides support for those strings which contains multibyte sequence in order to represent characters.
mbstring is a non-default extension. This means it is not enabled by default. You must explicitly enable the module with the configure option.
Like iconv you can see if your php has mbstring enabled looking at the phpinfo:

phpinfo mbstring

For a list of supported encodings see the mbstring page at php.net: http://php.net/mbstring

4.1.3 RECODE

This module contains an interface to the GNU Recode library. The GNU Recode library converts files between various coded character sets and surface encodings. When this cannot be achieved exactly, it may get rid of the offending characters or fall back on approximations. The library recognises or produces nearly 150 different character sets and is able to convert files between almost any pair
Also recode library is not a default php library.
To be able to use the functions defined in this module you must compile your PHP interpreter using the --with-recode[=DIR] option.
For string conversion it exposes to PHP the recode_string() method.
By the way, use iconv instead of recode for better results.

4.2 string phpCharset

The second parameter is the charset that the system assumes the PHP strings will be in.

4.3 string sqlCharset

sqlCharset is the charset of sql result sets used (only when outputting results to flash client)
We will see various examples of a combination of the character set later

5. The service class

What we need now is a simple service class which allows us to write and read strings from our mysql table.
Here's the php code for "tutorials/tutorial1.php" service class:


<?php

/**
 * Simple class for writing and reading strings
 * from mysql using different encoding and encoders
 * defined in the gateway class
 */
class tutorial1
{
    var 
$methodTable;
    var 
$db;

    function 
tutorial1()
    {
        
$this->db mysql_connect("localhost""""");
        
mysql_select_db("amfphp");
        
$this->methodTable = array(
                
"InsertString" => array(
                    
"access" => "remote",
                    
"description" => "Insert a new string in the database",
                    
"returntype" => "string"
                
),
                
"GetStrings" => array(
                    
"access" => "remote",
                    
"description" => "Get a list of available strings",
                    
"returntype" => "RecordSet"
                
),
        );
    }

    
/**
     * Insert a new string into the table
     */
    
function InsertString($string)
    {
        
$st addslashes($string);
        
$query sprintf("INSERT INTO table_strings (f_string) VALUES ('%s')"$st);
        if(@
mysql_query($query))
        {
            return 
"OK";
        } else {
            return 
$query " = " mysql_error();
        }
    }

    
/**
     * Get the last 30 inserted strings
     */
    
function GetStrings()
    {
        return 
mysql_query("SELECT id as STRING_ID, f_string as STRING_VALUE FROM table_strings ORDER BY id DESC LIMIT 0, 30");
    }
}
?>

There's nothing particular to say about this class. It simply connect to database and exposes 2 remote methods for flash remoting: InsertString and GetStrings