When I use PHP to manipulate the database today, I find that the Chinese characters inserted in the SQL Server 2008 database are garbled, and here are some of the things I started with:
The development environment is the encoding of the Php5.3.3+apache2.2.17+sql Server 2008,php script file is utf-8, the encoding passed to the database is GB2312 (the default character encoding for SQL Server may be this, I'm not sure), I use the official Microsoft provided sqlsrv library to connect to the database (Ps:sql Server 2005 has not supported the use of Mssql.dll to connect), so use sqlsrv_query ($conn, "Set names GB2312"); Statement to set the encoded format passed to the database, the SQL statement reads: INSERT into Opinion (content) VALUES (' AAA Chinese contents ');
Run this SQL statement, found that execution is unsuccessful, using the sqlsrv_errors () function to output error messages, the following results are obtained:
Copy CodeThe code is as follows:
Array ([0] = = Array ([0] = = IMSSP [SQLSTATE] = IMSSP [1] = -46 [code] = -46 [2] = an error Occurre D translating the query string to Utf-16:ڶֽڵŀҳуûдunicodeַӳ䵽ַ. [Message] = A error occurred translating the query string to Utf-16:ڶֽڵŀҳуûдunicodeַӳ䵽ַ. ) )
This is the results displayed on the Web page, the above garbled is copy down intact. From the "An error occurred translating the query string to UTF-16" You can see that the character encoding conversion is causing problems. So I use PHP's iconv function to force the conversion of the Chinese code, and then execute the SQL statement, the code is as follows:
Copy CodeThe code is as follows:
$string = Iconv (' utf-8 ', ' gb2312//ignore ', ' AAA Chinese content ');
$sql = "INSERT into Opinion (content) VALUES ($string)";
[Code]
The error message is as follows:
[Code]
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 ' mainland)
This error message can not see any clue, I again the SQL statement output to the Web page to see if the SQL statement is wrong, the output is as follows:
Copy CodeThe code is as follows:
Insert into Opinion (content) VALUES (AAA)
I look like no problem, in fact, there is a problem, notice that the parentheses in the back of the argument is to be enclosed in quotation marks (indicating that it is a string), so I modified the SQL statement, the code is as follows:
Copy CodeThe code is as follows:
$sql = "INSERT into Opinion (content) VALUES ('". $string. "')"; To get a better look, I'm magnifying.
Enclose the $string in single quotation marks so that the execution of the SQL statement succeeds, and the Chinese stored in the database is not garbled.
http://www.bkjia.com/PHPjc/325615.html www.bkjia.com true http://www.bkjia.com/PHPjc/325615.html techarticle today, using PHP to manipulate the database when the inserted SQL Server 2008 database in the text snippet is garbled, the following is my initial situation: the development environment is php5.3.3+apache2.2.17 ...