Garbled characters appear when php saves Chinese sql server 2008_PHP tutorial

WBOY
Release: 2016-07-13 10:46:43
Original
945 people have browsed it

The article collects some commonly used methods to solve the problem of garbled characters in PHP when saving Chinese SQL Server 2008. Friends who have encountered similar problems can refer to this article.

When I used PHP to operate the database today, I found that the Chinese fields inserted into the SQL Server 2008 database were garbled. Here are some of my initial situations:

The development environment is php5.3.3+Apache2.2.17+SQL Server 2008, the encoding of the php script file is utf-8, and the encoding passed to the database is GB2312 (the default character encoding of SQL Server may be this, I am not sure) , I use the SQLSRV library officially provided by Microsoft to connect to the database (PS: SQL Server 2005 no longer supports mssql.dll to connect), so I use the sqlsrv_query($conn, "set names GB2312"); statement. To set the encoding format passed to the database, the SQL statement is written like this: insert into Opinion (content) values ​​('aaa Chinese content');

Run this sql statement and find that the execution is unsuccessful. Use the sqlsrv_errors() function to output the error message and get the following results:

 代码如下 复制代码
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -46 [code] => -46 [2] => An error occurred translating the query string to UTF-16: �ڶ��ֽڵ�Ŀ�����ҳ�У�û�д� Unicode �ַ����ӳ�䵽���ַ� . [message] => An error occurred translating the query string to UTF-16: �ڶ��ֽڵ�Ŀ�����ҳ�У�û�д� Unicode �ַ����ӳ�䵽���ַ� . ) )

This is the result displayed on the web page. The garbled characters above are copied intact. From "An error occurred translating the query string to UTF-16" we can see that it is caused by a problem with character encoding conversion. So I used PHP's iconv function to force encoding conversion of Chinese, and then executed the SQL statement. The code is as follows:

 代码如下 复制代码
$string = iconv('utf-8', 'GB2312//IGNORE', 'aaa中文内容');
$sql    = "insert into Opinion (content) values ( $string)";

At this time, another error was reported. The error message is as follows:

 代码如下 复制代码
Array ( [0] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]���� 'aaa��������' ���� [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]���� 'aaa��������' ���� ) )

There is no clue about this error message. I output the sql statement to the web page to see if the sql statement was written incorrectly. The output result is as follows:

 代码如下 复制代码
insert into Opinion (content) values ( aaa��������)

At first glance, it seemed like there was no problem, but actually there was. I noticed that the parameter in the brackets at the back should be enclosed in quotation marks (indicating that it is a string), so I modified the sql statement and the code As follows:

 代码如下 复制代码
$sql = "insert into Opinion (content) values ( '".$string."')";   为了看清楚我放大点

Enclose $string in single quotes, so that the sql statement will be executed successfully and the Chinese saved in the database will not be garbled.


Refer to other solutions


If the encoding of the database is GB2312, how to configure it in Yii so that the data on the page can be displayed normally and the data in the database cannot be garbled:

Method 1:

1. The default encoding method of Yii is utf-8. If you want to change the encoding method, you need to add 'charset'=>'GB2312' in the main.php file, so as to change the encoding method of the function output
2. The encoding method of sql server 2000 defaults to the local GB2312 encoding method. Pay attention to the encoding method when operating the database
3. The encoding of the web page needs to be changed to GB2312
4. When saving the file, it needs to be saved in GB2312 encoding so that the Chinese characters in the php file can be parsed normally

Method 2:

1. Transcode data through iconv function

 代码如下 复制代码
iconv('GB2312','UTF-8',$data)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632931.htmlTechArticleThe article collects some commonly used methods to solve the problem of garbled characters in php when saving Chinese sql server 2008. Anyone who has encountered similar problems Friends can refer to this article. Today when using php to operate the database, I found that...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template