gpt4 book ai didi

php - 无法在 MySQL 中存储 UTF8 字符

转载 作者:行者123 更新时间:2023-11-29 01:07:02 25 4
gpt4 key购买 nike

无法找到无法在 MySQL 数据库中存储 ţ、î、ş 等字符的原因。

我的表定义是:

CREATE TABLE IF NOT EXISTS `gen_admin_words_translated` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`word_id` int(10) NOT NULL,
`value` text COLLATE utf8_unicode_ci,
`lang_id` int(2) NOT NULL,
`needUpd` int(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2689 ;

与数据库的连接是通过以下脚本完成的:

$charset = "UTF8";
$link = mysql_connect($host, $user, $pass);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}

我在页面的头部:

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

脚本是:

$text = 'ţ, î, ş';
mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");

我最后在表格中得到的是:

SELECT * FROM  `gen_admin_words_translated` 

id word_id value lang_id needUpd
5166 1034 ?, 1 1

最佳答案

当我运行你的脚本时,它对我有用:

$charset = "UTF8";
$link = mysql_connect('localhost', 'root', '') or die('connection?');
mysql_select_db('test') or die('database?');
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}

$text = 'ţ, î, ş';
mysql_query("insert into gen_admin_words_translated (word_id, lang_id, value, needUpd) values (1, 1, '$text', 1)");

$query = mysql_query('SELECT * FROM `gen_admin_words_translated`');
$array = mysql_fetch_array($query);

print_r($array)

结果:

Array
(
[0] => 2689
[id] => 2689
[1] => 1
[word_id] => 1
[2] => ţ, î, ş
[value] => ţ, î, ş
[3] => 1
[lang_id] => 1
[4] => 1
[needUpd] => 1
)

检查事项:

检查您的网页是否真的是 UTF-8,也许您在其他地方设置了一些 chaset。

header('Content-type: text/html; charset=utf-8');

文件编码也应该是 UTF-8,否则它可能会破坏你的字符......

关于php - 无法在 MySQL 中存储 UTF8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7441418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com