按字母顺序排列阿拉伯数据 [英] Order Arabic data by alphabet letters
问题描述
如何订购用阿拉伯字母从MySQL抓取的数据?
How can I order data grabbed from MySQL by Arabic alphabet ?
注意:我在MySQL中使用utf8_general_ci
作为排序规则,并且编码很好,我只想知道如何按字母对数据进行排序.
Note: I use utf8_general_ci
as collation in MySQL, and the encoding is fine, I just want to know how to sort data by alphabet.
推荐答案
为了使用php在mysql数据库中对阿拉伯文本进行排序,您必须确保:
In order to sort Arabic text in mysql database using php, you have to make sure that:
- MySQL字符集:
UTF-8 Unicode
(utf8
) - MySQL连接排序规则:
utf8_general_ci
- 您的数据库和表归类设置为:
utf8_general_ci
或utf8_unicode_ci
- MySQL charset:
UTF-8 Unicode
(utf8
) - MySQL connection collation:
utf8_general_ci
- Your database and table collations set to:
utf8_general_ci
orutf8_unicode_ci
因此,在创建表时,将字符集设置为utf8:
So when your create your table set charset to utf8 :
CREATE TABLE my_table (
id int(11) NOT NULL auto_increment,
name varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
如果您已经有一个用latin1
编码的表,请像这样转换它:
If you have already a table encoded in latin1
, convert it like this :
ALTER TABLE `my_table` CONVERT TO CHARACTER SET utf8
请参阅:在MySQL中设置的字符
然后,在设置数据库连接时,需要在PHP中添加一行:
Then, you need to add a line in your PHP when you setup the database connection :
mysql_query ('SET NAMES \'UTF8\'');
或:
mysql_set_charset ('UTF8'); // PHP 5.2.3 or greater.
或者,如果您使用的是PDO
,则可以将其作为第4个参数传递:
Or if you're using PDO
, you can pass it as a 4th parameter like this :
$db = new PDO ('mysql:host=localhost;dbname=tests', 'root', '',
array (PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
也不要忘记将页面标题设置为utf8
:
Also don't forget to set the header of your pages to utf8
:
header ('Content-type:text/html; charset=utf-8');
示例排序查询:
$sql = "SELECT * FROM my_table ORDER BY name" ;
希望这会有所帮助.
这篇关于按字母顺序排列阿拉伯数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!