How to store multi byte characters in SQL Server database using CodeIgniter

穿精又带淫゛_ 提交于 2019-12-04 06:15:11

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

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

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.

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

diegolaprida

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.

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

$value = htmlspecialchars($input)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!