无法获得MySQL中的编码权限 [英] Can't get the encoding right in MySQL

查看:106
本文介绍了无法获得MySQL中的编码权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决MySQL中的编码问题。我正在建立一个数据库,将不仅包含拉丁文,但西里尔文和阿拉伯语文本。下面是一个关于如何创建数据库的示例:

I have been struggling with encoding problems in MySQL for a while. I am building a database that will contain not only Latin but Cyrillic and Arabic text as well. So here is an example on how I create the database:

CREATE DATABASE db1
DEFAULT CHARACTER SET utf8   
COLLATE utf8_unicode_ci;

然后一个表格:

CREATE TABLE TempTb1
(
ID INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
arabic VARCHAR(100) NOT NULL
)
DEFAULT CHARACTER SET utf8   
COLLATE utf8_unicode_ci;

当我放一些数据并选择它时,我只得到一些奇怪的字符。所以我写了一个小的PHP脚本来测试它,但它不工作:

And when I put some data and select it I get only some strange characters. So I wrote a small PHP script to test it but it doesn't work either:

<?php
header('Content-type: text/plain; charset=utf-8');

$a = mysql_connect('localhost','root','') or die('Problem connecting to database!');
$b = mysql_select_db('db1') or die('Problem selecting database');
mysql_set_charset('utf8');
mysql_query("set names 'utf8'");
mysql_query('set character set utf8');

$query = mysql_query("SELECT * FROM Tb1;");

while($row = mysql_fetch_assoc($query))
    {   
        $id = $row['ID'];
        $name = $row['name'];
        $arabic = $row['arabic'];

        echo $id.' '.$name.' '.$arabic.PHP_EOL;
    }
?>



我已经测试了 utf8_unicode_ci code> utf8_general_ci 。什么可能是错误? BTW我有EasyPHP 5.2.10。

I have tested with both utf8_unicode_ci and utf8_general_ci. What could be wrong? BTW I have EasyPHP 5.2.10.

推荐答案

无论发生什么,你的字符,发生在他们到达MySQL之前,我猜。当我们输入字符时,字符由计算机转换为数字。然后,这些数字从这里到那里,在Web表单和服务器,Web服务器和脚本解释器之间,然后数据库服务器和回到网页以相同的方式。

Whatever happens to your characters, happens before they reach to MySQL, I guess. Characters are converted to numbers by the computer when we enter the characters. Then these numbers travel from here to there, between web forms and servers, web servers and scripting interpreters, then database servers and back to web pages following the same way.

以及如何输入您的数据?数据应该退出它输入的方式。如果您的数据是通过网络表单提供的,请检查您的网页编码和如何提交表单。如何在PHP脚本中获取它们以及如何将它们发送到数据库服务器。这里有罪的部分可能不是MySQL,而是另一个地方。它可以是MySQL;

Where and how you enter your data? Data should exit the way it entered. If your data is provided via web forms, check your web page encodings and how you submit forms. How you get them in your PHP scripts and how you send them to database server. The guilty part here is probably not MySQL but another place. It can be MySQL too; but it is not the only place of possible misbehavior and it probably is not.

检查您的网页,在到达您的浏览器时检查标题。

Check your pages, check headers as they arrive to your browser.

关于您的问题的意见,没有使用ISO5不好,因为您需要多个ISO5系列。你必须使用Unicode编码,大多数时候,最好是utf-8。此外,这不是关于你使用哪个MySQL库,除非该库有一些已知的错误,这是不太可能的旧的东西。 :)你应该仍然使用建议的最佳做法;但您当前的问题与您使用的库无关。这种邪恶与您输入数据的方式和查看方式之间的区别。

About comments your question received, no it is not good to use ISO5 because you need multiple of ISO5 families. You must go with a Unicode encoding, for most of the time, the best being utf-8. Also, this is not about which MySQL library you use unless that library has some known bugs which is very unlikely for something that old. :) You should still use whatever recommended as best practices; but your current problem is not related to the library you use. The evil is at the difference between how you enter your data and how you view it.

这篇关于无法获得MySQL中的编码权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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