在mysql表中保存Ø而不是Ã〜

时间:2011-04-23 02:36:37

标签: php mysql character-encoding special-characters character

我的表格如下:

CREATE TABLE `friends` 
(
    `id` bigint(20) NOT NULL default '0',
    `fb_id` bigint(20) NOT NULL default '0',
    `name` varchar(100) NOT NULL default '',

) TYPE=MyISAM charset=utf8;

我像这样添加到数据库中:

            $sql = "insert ignore into `friends` (`id`, `fb_id`, `name`)" .
                            "values (:id, :fb_id, :name)";
                try 
                {
                    $stmt = $this->db->prepare($sql); 
                    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
                    $stmt->bindParam(':fb_id', $fb_id, PDO::PARAM_INT);
                    $stmt->bindParam(':name', $name, PDO::PARAM_STR);

                    $result = $stmt->execute();
                    $stmt->closeCursor(); 
                }       
                catch (Exception $e)
                {
                die ($e->getMessage() ); 
                }

如何保存Øystein而非Øystein。我已将字符集设置为utf8,不确定我还能做些什么。

编辑:

升级了我的db_connect函数:

        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
        $driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8' );

        try
        {
            $this->db = new PDO($dsn, DB_USER, DB_PASS, $driver_options);
        }

仍然遇到同样的问题..

4 个答案:

答案 0 :(得分:0)

尝试在连接到数据库后添加$this->db->exec('SET CHARACTER SET utf8')

答案 1 :(得分:0)

您是否告诉浏览器在页面上使用哪种文本编码,提交表单和页面,稍后会显示从数据库中检索到的结果?

在HTML页面的标题中应显示以下内容:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  

答案 2 :(得分:0)

答案 3 :(得分:0)

您是否尝试过将字符集添加到DSN,如下所示:

$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ';charset=UTF-8';

您是否可以运行此测试代码以进一步追踪问题?

<?php

echo '<html>'
   . '<head>'
   . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'  
   . '</head>'
   . '<body>'
   ;

error_reporting(E_ALL);
ini_set("display_errors", 1);

define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'test' );
define( 'DB_USER', 'root' );
define( 'DB_PASS', 'root' );

$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ';charset=UTF-8';

$db = new PDO($dsn, DB_USER, DB_PASS ); 

$id = 12;
$fb_id = '23';

if ( ! isset( $_POST[ 'name'] )) {

    echo '<form method="POST" >Strange letters go here <input type="text" name="name"></form>'
       . '</body>' 
       ;
    exit;
}

$name = $_POST[ 'name'];


$sql = "insert ignore into `friends` (`id`, `fb_id`, `name`)" .
                            "values (:id, :fb_id, :name)";
try
{
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->bindParam(':fb_id', $fb_id, PDO::PARAM_INT);
    $stmt->bindParam(':name', $name, PDO::PARAM_STR);

    $result = $stmt->execute();
    $stmt->closeCursor();

    echo 'OK';
}
catch (Exception $e)
{
    die ($e->getMessage() );
}

$sql = 'SELECT *'
     . 'FROM `friends` '
     ;

try
{
    $stmt = $db->prepare($sql);

    $result = $stmt->execute();

    while( $result = $stmt->fetch(PDO::FETCH_ASSOC) ) {

        echo '<br />' . print_r( $result );

    }

    $stmt->closeCursor();

    echo 'OK';
}
catch (Exception $e)
{
    die ($e->getMessage() );
}


echo '<body>';

如果我提交此

öäüÖÄÜß

页面会读取并发回此

öäüÖÄÜß

虽然我在phpMyAdmin中看到这个(可能是phpMyAdmin中的一个错误):

öäüÃ-A“Üß