如何处理数据库中的重音和奇怪字符? [英] how to deal with accents and strange characters in a database?

查看:347
本文介绍了如何处理数据库中的重音和奇怪字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的数据库安全的西班牙语单词与口音,但它不会工作,我已经尝试:

im trying to safe spanish words with accent in my database but it won't work, i have already tried:

1)更改conllation从表和行 utf8_spanish_ci utf_unicode_ci。

1) changing conllation from tables and rows to utf8_spanish_ci and utf_unicode_ci.

2)标记与

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

3)添加

header("Content-Type: text/html;charset=utf-8");

这在我的笔记本电脑中的xampp服务器将工作,但是当我上传数据库到一个登录怪物服务器它不会保存口音正确。

doing this in an xampp server in my laptop will work, but when i upload the database to a login monster server it wont save the accent properly.

编辑:这是连接im使用:

edit: this is the connection im using:

    private function Connect()
    {
        //$this->settings = parse_ini_file("settings.ini.php");
        try 
        {
            # Read settings from INI file, set UTF8
            $this->pdo = new PDO('mysql:host=localhost;dbname=xxxxx;charset=utf8', 'xxxxx', 'xxxxxx', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

            # We can now log any exceptions on Fatal error. 
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            # Disable emulation of prepared statements, use REAL prepared statements instead.
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

            # Connection succeeded, set the boolean to true.
            $this->bConnected = true;
        }
        catch (PDOException $e) 
        {
            # Write into log
            echo $this->ExceptionLog($e->getMessage());
            die();
        }
    }

编辑:

我不能保存口音,它显示像á=¡

i can't save accent, it shows like strange characters like á = á

推荐答案

> Collat​​ion 仅影响文本排序,它对存储数据的实际字符集没有影响。

Collation affects text sorting only, it has no effect on actual character set of stored data.

我会建议使用此配置:


  1. 仅为整个数据库设置字符集,因此不必为每个表单独设置。字符集从DB继承到表到列。使用 utf8 作为字符集。

  1. Set the character set for the whole DB only, so you don't have to set it for each table separately. Character set is inherited from DB to tables to columns. Use utf8 as the character set.

设置数据库连接的字符集。在连接到数据库之后执行这些查询:

Set the character set for the DB connection. Execute these queries after you connect to the database:

SET CHARACTER SET 'utf8'
SET NAMES 'utf8'


  • 为页面设置字符集 /或HTML元标记。其中一个就够了。使用 utf-8 作为字符集

  • Set the character set for the page, using HTTP header and/or HTML meta tag. One of these is enough. Use utf-8 as the charset.

    如果要对西班牙字符串进行正确排序,请设置 collat​​ion 为整个数据库。 utf8_spanish_ci 应该可以使用( ci 表示不区分大小写)。没有正确的排序规则,重音西班牙字符将总是排序最后。

    If you want to have proper sorting of Spanish strings, set collation for the whole database. utf8_spanish_ci should work (ci means Case Insensitive). Without proper collation, accented Spanish characters would be sorted always last.

    注意:您可能已经在表被破坏,因为你的字符集配置先前是错误的。您应该首先使用一些DB客户端来检查它,以排除这种情况。如果已损坏,只需使用正确的字符集配置重新插入数据即可。

    Note: it's possible that the character set of data you already have in a table is broken, because you character set configuration was wrong previously. You should check it using some DB client first to exclude this case. If it's broken, just re-insert your data with the right character set configuration.


    • objects have a character set attribute, which can be set explicitly or it's inherited (server > database > table > column), so the best option is to set it for the whole database

    客户端连接 还有一个字符集属性,它告诉数据库发送数据的编码

    client connection has also a character set attribute and it's telling the database in which encoding you're sending the data

    如果客户端连接和目标对象的字符集不同,则要发送到数据库的数据将从连接的字符集自动转换为对象的字符集。

    If client connection's and target object's character sets are different, the data you're sending to the database are automatically converted from the connection's character set to the object's character set.

    所以,如果你有例如 utf8 中的数据,但是客户端连接设置为 latin1 ,数据库将中断数据,因为它会尝试转换 utf8 ,例如 latin1

    So if you have for example the data in utf8, but client connection set to latin1, the database will break the data, because it'll try to convert utf8 like it's latin1.

    这篇关于如何处理数据库中的重音和奇怪字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆