Php mssql extension SQL query Chinese field name Solution

Source: Internet
Author: User

I. Problems:

The database is MS SQLServer2000. You need to import data from a table in SQLServer2000 to mysql5. the fields in the SQLServer2000 table are named in simplified Chinese (it is strongly recommended that the Field name be Chinese ). In fact, the operation is to query SQL server records and insert them into MySQL. The selected scripting language is PHP. Enabling MSSQL and MySQL extensions in PHP makes operations on these two databases very easy.
The problem occurs when the field name of the table in SQL Server is Chinese. The written query statement is tested in SQL Server and returned by a record. An error is returned when the SQL extension query in PHP is used.
I checked the information online. There is not much information on the Internet. Many netizens think that PHP's MSSQL extension does not support Chinese Characters in SQL statements. After checking the information, php mssql supports Chinese statements in SQL. Most of the errors are caused by encoding. The encoding database and the encoding are inconsistent with the query statement encoding, and the Chinese part of the query statement in SQLServer becomes garbled, resulting in query failure.

Ii. solution:

After knowing the cause, analyze and solve the problem and confirm that the encoding is not uniform. The solution involves the following steps:
1. Confirm the encoding of the SQLServer database. My data encoding is GBK.
2. Confirm the encoding of the current PHP script file. My encoding is UTF-8.
3. Convert the encoding of the SQL query statement.
Supplement: Some netizens mentioned that PHP script file encoding should be consistent with the database encoding. In fact, this step is not necessary, as long as you confirm that your SQL statement is consistent with the database encoding, we recommend that you do not need to convert it because if your PHP script file contains other PHP scripts, you must encode and convert all include or require script files, otherwise, PHP script encoding is not uniform and prone to errors. If there are many files associated with each other, this is also a very troublesome problem and complicated things.

Iii. solution:

Write a conversion function to encode and convert SQL statements before performing SQL operations. The following is my sample code:
Copy codeThe Code is as follows:
// Encoding conversion function
Function utf8togb ($ s ){
Return iconv ('utf-8', 'gbk // IGNORE ', $ s); // when the IGNORE parameter is a character that cannot be converted, IGNORE
}
// We recommend that you replace all Chinese fields with English aliases to facilitate encoding and conversion in the following operations.
$ SQL = "SELECT [id], [column] as typeid, [subject] as title, [author] as author, convert (text, [body]) as body FROM [Document Table]; ";
$ SQL = utf8togb ($ SQL );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.