PHP connection MSSQL is garbled when displaying Chinese Characters
When using PHP to connect MSSQL to display Chinese characters, garbled characters are mainly caused by encoding problems. We only need to know how to handle the encoding of databases and pages in a unified manner. Let's take a look at the following.
Because I have been using PHP development in windows, using mysql databases, and occasionally encountering garbled characters in query records, it is because php encoding does not support Chinese characters, it can be directly decoded, so it is easier to solve in windows.
Today, I am helping a customer maintain the mssql database under liunx. Due to the attack on the site, I have been unable to open the site. After a long time, I finally connected to mssql, let's try the query today. What's the problem!
What is the Chinese display of the records? Number or a bunch of black boxes. This is a typical Incorrect code, so I checked the php. this configuration item of ini about mssql is found to be "mssql. charset "is configured as" utf8 "and garbled characters are successfully resolved.
Because my program page uses utf8, it should be configured as follows. If it is GBK/GB2312, it should be able to solve the problem by configuring the corresponding encoding length, of course, we still need to look at the corresponding situation when encountering such a problem. We recommend that you modify the configuration file as much as possible, because this will solve the problem once and for all.
Example
The Code is as follows: |
|
$ ServerName = "127.0.0.1, 1433 "; $ ConnectionInfo = array ("UID" => "sa ", "PWD" => "123456 ", "Database" => "NopCommerce ", "CharacterSet" => "UTF-8"); // This line is important $ Conn = sqlsrv_connect ($ serverName, $ connectionInfo ); |
Note:
Coding commonly used is utf8-general-ci
Not only tables must be unified, but a total of four tables must be unified.
First, the database types, including databases, tables, and fields, must be unified. Check the fields.
Second, the file encoding type. If you use dw or editplus, you can view the page encoding.
Third, set NAMES utf8 when accessing the database;
4. Add the meta attribute in the browser display mode.
No matter how GBK or GB2312 is used, the steps must be unified.
Using PHP to operate MSSQL is easier than connecting to MYSQL in ASP. Therefore, when MSSQL and MYSQL are required to coexist, it is easier to use PHP to connect MSSQL to operate the coexistence of MYSQL and MSSQL. if ASP is used to connect to MYSQL, you need to install a MYSQL driver. By default, ODBC in windows is not installed. Sorry...
1. Install at least mssql client on the web Server
2. Open php. ini and remove the semicolon before extension = php_mssql.dll.
Required: extension_dir
3. We recommend that you use php <= 4.0.9 <= 5.0.3. Currently, I have not successfully connected php 4.010 or 5.0.3.
4. You can go to phpe.net to obtain the corresponding class on the database connection page.