ujisなMySQLにutf-8なデータをつっこむ。

mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+-----------------------------------------------+
| Variable_name            | Value                                         |
+--------------------------+-----------------------------------------------+
| character_set_client     | ujis                                          |
| character_set_connection | ujis                                          |
| character_set_database   | ujis                                          |
| character_set_results    | ujis                                          |
| character_set_server     | ujis                                          |
| character_set_system     | utf8                                          |
+--------------------------+-----------------------------------------------+
7 rows in set (0.00 sec)

な、MySQLutf-8のDBを作成し、

$ php -i |grep "mbstring?."
mbstring.detect_order => no value => no value
mbstring.encoding_translation => Off => Off
mbstring.func_overload => 0 => 0
mbstring.http_input => auto => auto
mbstring.http_output => EUC-JP => EUC-JP
mbstring.internal_encoding => EUC-JP => EUC-JP
mbstring.language => Japanese => Japanese
mbstring.script_encoding => no value => no value
mbstring.substitute_character => no value => no value

な、phputf-8なデータをインサートする。

<?php
mb_http_output( 'UTF-8' );
mb_internal_encoding( 'UTF-8' );
mb_http_input( 'pass' );

$urf_str = 'utf-8な文字列';

$db = mysql_connect( 'localhost', 'user', 'pass' );
mysql_select_db( 'utf_db' );

// 重要
mysql_query( 'SET NAMES utf8' );
if ( ! get_magic_quotes_gpc() ) {
    $urf_str = addslashes( trim( $urf_str ) );
}

$query = "INSERT INTO utf_table ( utf_col ) VALUES ( '$urf_str' ) ";
mysql_query( $query );
mysql_close();
?>