Home >Backend Development >PHP Tutorial >PHP displays garbled characters when connecting to MSSQL_PHP Tutorial
When PHP connects to MSSQL and displays Chinese characters, it displays garbled characters. The main reason is because of encoding problems. We only need to know how to handle the encoding of databases and pages in a unified manner. It can be solved, let’s take a look below.
Since I have been using PHP development under Windows and using the mysql database, I occasionally encounter garbled query records. This is because PHP encoding does not support Chinese. It can be solved by encoding and decoding it directly. , so it is relatively easy to solve under the windows platform.
Today, I was helping a customer maintain the mssql database under liunx. Due to the site being attacked, I couldn’t open the site. After struggling for a long time, I finally connected to mssql. I came today and tried to query. What’s the problem!
But the Chinese characters of the records displayed are ? numbers or a bunch of black boxes. This is a typical coding error, so I checked the configuration of mssql in php.ini and found that there is a configuration "mssql.charset" For one item, I configured it as "utf8" and successfully solved the garbled characters.
Because my program page uses utf8, it is configured like this. If it is GBK/GB2312, configuring the corresponding encoding should be able to solve it. Of course, if you encounter such a problem, you still need to look at the corresponding situation. Here is still recommended if You can modify the configuration file as much as possible, because this can solve the problem once and for all.
Example
The code is as follows | |||||
$serverName = "127.0.0.1,1433";
|
Notes:
The commonly used encoding is utf8-general-ci
It’s not just the tables that need to be unified, there are four aspects that need to be unified
The first is: database type, including database, table, and fields, which must be unified. You can check it
The second is: the encoding type of the file. If you use dw or editplus, you can check the page encoding. If it differs, you need to modify it
The third is: the settings when accessing the database are set NAMES utf8;
The fourth is: browser display mode, add meta attribute
Whether you use GBK or GB2312, these four aspects must be unified. It depends on which step you are missing
Using PHP to operate MSSQL is easier than connecting to MYSQL in ASP. Therefore, when MSSQL and MYSQL need to coexist, it is easier and easier to use PHP to connect to MSSQL to operate MYSQL and MSSQL. If ASP is connecting to MYSQL, you need to install a MYSQL Driver, the default Windows ODBC is not installed, unfortunately...
1. Install at least mssql client on the web server
2. Open php.ini and remove the semicolon in front of extension=php_mssql.dll
If necessary: extension_dir needs to be specified
3. It is recommended to use php<=4.0.9<=5.0.3. Currently, I have not successfully connected to 4.010 and 5.0.3
4. For database connection paging, you can get the corresponding class on phpe.net