如何使用 CodeIgniter 在 SQL Server 数据库中存储多字节字符

发布于 2025-01-03 20:45:07 字数 1614 浏览 0 评论 0原文

我正在使用 MS SQL Server 和 CodeIgniter 2 以及 Active Record 来处理我正在处理的项目,我偶然发现了这个问题:

当我提交包含中文或印地语字符的表单时,我将其存储在表中,并且当我看到它时,我得到的都是问号。如果我尝试英语或希腊字符,一切似乎都正常。

我相信这与我正在编写的 PHP 有关,因为如果我直接在 SQL Server Management Studio 中复制粘贴中文文本,所有值都会完美地存储和显示,无论是在 SQL Studio 上还是在 SQL Server 上。网络应用程序。

这些是我正在使用的数据库设置:

$db['local']['dbdriver'] = 'sqlsrv';
$db['local']['dbprefix'] = '';
$db['local']['pconnect'] = FALSE;
$db['local']['db_debug'] = TRUE;
$db['local']['cache_on'] = FALSE;
$db['local']['cachedir'] = '';
$db['local']['char_set'] = 'utf8';
$db['local']['dbcollat'] = 'utf8_general_ci';
$db['local']['swap_pre'] = '';
$db['local']['autoinit'] = TRUE;
$db['local']['stricton'] = FALSE;

这是我现在正在测试的表的结构:

CREATE TABLE [dbo].[languages](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [language] [nvarchar](1024) NULL,
    [language_local] [nvarchar](1024) NULL,
    [lang_code] [nvarchar](100) NULL,
    [core] [bit] NULL,
 CONSTRAINT [PK_languages] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,         ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

这是我在 config.php 中的字符集编码

$config['charset'] = 'utf-8';

新的故障排除数据

我尝试保存我的表单中包含以下字符串: Iñtërnâtiônàlizætiøn

CodeIgniter 回复了此错误:

An error occurred translating the query string to UTF-16: No mapping for the Unicode character exists in the target multi-byte code page. .

当我尝试存储中文字符时,不会出现此错误 先感谢您 :)

I'm using MS SQL Server and CodeIgniter 2 with Active Record for a project I'm working on, and I just stumbled upon this issue:

When I submit a form that contains Chinese or Hindi characters, I store it in a table, and when I view it all I get are question marks. If I try English or Greek characters, everything seems to work fine.

The reason I believe this is something to do with the PHP I'm writing, is because if I copy-paste the chinese text directly in SQL Server Management Studio, all values are stored and displayed perfectly, both on the SQL Studio, and the web application.

These are the db settings I'm using:

$db['local']['dbdriver'] = 'sqlsrv';
$db['local']['dbprefix'] = '';
$db['local']['pconnect'] = FALSE;
$db['local']['db_debug'] = TRUE;
$db['local']['cache_on'] = FALSE;
$db['local']['cachedir'] = '';
$db['local']['char_set'] = 'utf8';
$db['local']['dbcollat'] = 'utf8_general_ci';
$db['local']['swap_pre'] = '';
$db['local']['autoinit'] = TRUE;
$db['local']['stricton'] = FALSE;

This is the structure of the table I'm testing on right now:

CREATE TABLE [dbo].[languages](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [language] [nvarchar](1024) NULL,
    [language_local] [nvarchar](1024) NULL,
    [lang_code] [nvarchar](100) NULL,
    [core] [bit] NULL,
 CONSTRAINT [PK_languages] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,         ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

And this is my charset encoding in config.php

$config['charset'] = 'utf-8';

New troubleshooting data

I tried to save the following string through my form: Iñtërnâtiônàlizætiøn

CodeIgniter replied with this error:

An error occurred translating the query string to UTF-16: No mapping for the Unicode character exists in the target multi-byte code page. .

This doesn't appear when I try to store Chinese characters
Thank you in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

菩提树下叶撕阳。 2025-01-10 20:45:07

在插入 db 之前尝试使用 iconv() 转换您的输入:

$input = iconv('','UTF-8',$str);

Try to convert your input with iconv() before insert to db :

$input = iconv('','UTF-8',$str);
鱼窥荷 2025-01-10 20:45:07

从 PHP 处理 Microsoft SQL Server 中的编码可能会非常痛苦。 CharacterSet 选项包含在 Microsoft SQL Server Driver for PHP (SQLSRV) 1.1 版中,因此您很有可能使用的是不支持设置 ChracterSet 的过时版本,尽管这种情况不太可能发生。不能将 char_set 更改为 UTF-16,因为 SQLSRV 仅支持 UTF-8。

更可能的是以下情况之一适用:

  • 在您的 php.ini 中,选项 default_charset 未设置为 UTF-8,
  • 因为您可能在 Windows 计算机上工作,您的 .php 文件未以 UTF 编码-8。

如果这不能解决问题,则您的输入可能包含一个或多个字符,这些字符不是有效的 UTF-8。在这种情况下,请尝试使用 iconv() 转换您的(用户)输入。

编辑:关于@Markus评论:CodeIgniter的 system/database /drivers/sqlsrv/sqlsrv_driver.php 看起来像是 sqlsrv-commands,因此似乎不太可能,那个问题
是由 CodeIgniter 代码引起的。

Handling encoding in Microsoft's SQL Server from PHP can be quite painful. The CharacterSet-option was included with version 1.1 of Microsoft SQL Server Driver for PHP (SQLSRV), so there's an off-chance, you are using an outdated version that does not support setting the ChracterSet, although that is unlikely. Changing char_set to UTF-16 is not an option, as SQLSRV only supports UTF-8.

More likely one of the following applies:

  • in your php.ini the option default_charset is not set to UTF-8
  • as you probably are working on a Windows machine, your .php-file is not encoded in UTF-8.

If this does not solve the problem, then your input probably contains one ore more characters, which are not valid UTF-8. In this case try converting your (user) input with iconv().

edit: Regarding @Markus comment: CodeIgniter's system/database/drivers/sqlsrv/sqlsrv_driver.php looks like a simple wrapper around the sqlsrv-commands, it seems therefore unlikely, that the problem
is caused by CodeIgniter-code.

岁月蹉跎了容颜 2025-01-10 20:45:07

看起来这个答案引起了很多关注,我为没有发布问题的实际解决方案而感到难过...我想取消选择我多年前选择的答案是不礼貌的,所以我暂时不会。这里...

无需对设置进行任何更改。该问题与查询相关,不幸的是 CodeIgniter 不支持开箱即用的正确查询格式。

因此,当您想要在表中插入多字节字符时,必须在字符串前面添加字符 N

所以在我上面的例子中,查询应该像这样才能工作

INSERT INTO test_table (title) VALUES (N'Iñtërnâtiônàlizætiøn')

不,CI 目前没有给你一个内置的方法来做到这一点。它计划添加到 CI4 上,但在那之前这里有一个给你的破解< /a>

Looks like this answer is getting a lot of attention, and I feel bad for not posting the actual solution to my problem... I'd guess it's bad etiquette to de-select an answer I selected many years ago so I won't for now. Here goes...

No changes needed to be done to the settings. The problem is query related, and unfortunately CodeIgniter doesn't support the proper query format out of the box.

So when you want to insert multibyte characters into your table, you have to prepend the character N before your string.

So in my example above the query should look like this in order to work

INSERT INTO test_table (title) VALUES (N'Iñtërnâtiônàlizætiøn')

No, CI doesn't currently give you a built in way to do this. It is planed to be added in on CI4, but until then here is a hack for you

爱本泡沫多脆弱 2025-01-10 20:45:07

我遇到了这个错误:

错误号:IMSSP

将查询字符串转换为 UTF-16 时发生错误:无法在 Unicode 字符的目的地多个字节的页面上指定该字符。 。

对于我来说,工作人员:

iconv('','utf-8',$pass);

在数据库上进行查询之前。

I had this error:

Error Number: IMSSP

An error occurred translating the query string to UTF-16: No hay ninguna asignación en la página de códigos de múltiples bytes de destino para el carácter Unicode. .

And for me workrs with:

iconv('','utf-8',$pass);

before doing the query on the DB.

一口甜 2025-01-10 20:45:07

我遇到了同样的问题,但只有 htmlspecialchars() 有效我。

$value = htmlspecialchars($input)

I had same problem but only htmlspecialchars() worked for me.

$value = htmlspecialchars($input)
庆幸我还是我 2025-01-10 20:45:07

尝试使用 utf-16 编码,在 :

$config['charset'] = 'utf-16';

$db['local']['char_set'] = 'utf16';

中,并且 db 编码必须正确设置为 utf-16。

Try with utf-16 encoding, both in :

$config['charset'] = 'utf-16';

and

$db['local']['char_set'] = 'utf16';

And also the db encoding must be set properly as utf-16.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文