选择带有日语字符的MySQL行 [英] Select MySQL rows with Japanese characters
问题描述
有人会知道一种可靠的方法(使用mySQL或其他方法)来选择数据库中包含日语字符的行吗?我的数据库中有很多行,其中一些仅包含字母数字字符,其中一些具有日语字符.
Would anyone know of a reliable method (with mySQL or otherwise) to select rows in a database that contain Japanese characters? I have a lot of rows in my database, some of which only have alphanumeric characters, some of which have Japanese characters.
推荐答案
想要对字符集有问题的规则:
Rules when you want to have problem with character sets:
-
在创建数据库时使用utf8编码:
when creating database use utf8 encoding:
CREATE DATABASE _test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
确保所有文本文件(varchar和text)都使用UTF-8:
Make sure all text fileds (varchar and text) are use UTF-8:
CREATE TABLE _test.test (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
建立连接时,请在查询/更新数据库之前执行以下操作:
When you make a connection do this before you query/update the database:
SET NAMES utf8;
使用phpMyAdmin-登录时选择UTF-8.
With phpMyAdmin - Choose UTF-8 when you login.
将网页编码设置为utf-8,以确保所有发布/获取数据都采用UTF-8(否则您将不得不痛苦地进行转换.). PHP代码(php文件中的第一行或至少在任何输出之前):
set web page encoding to utf-8 to make sure all post/get data will be in UTF-8 (or you'll have to converting is painful..). PHP code (first line in the php file or at least before any output):
header('Content-Type: text/html; charset=UTF-8');
确保所有查询均以UTF8编码编写.如果使用PHP:
Make sure all your queries are written in UTF8 encoding. If using PHP:
6.1.如果PHP支持UTF-8中的代码-只需使用UTF-8编写文件即可.
6.1. If PHP supports code in UTF-8 - just write your files in UTF-8.
6.2.如果php是在没有UTF-8支持的情况下编译的,则将字符串转换为UTF-8,如下所示:
6.2. If php is compiled without UTF-8 support - convert your strings to UTF-8 like this:
$str = mb_convert_encoding($str, 'UTF-8', '<put your file encoding here');
$query = 'SELECT * FROM test WHERE name = "' . $str . '"';
应该这样做.
这篇关于选择带有日语字符的MySQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!