PHP MSSQL Extension SQL query text segment name Workaround _php Tutorial

Source: Internet
Author: User
Tags sql server query mssql
first, the question:

The database is Ms SQLServer2000, to import data from a table in SQLServer2000 into MySQL5, where the fields of the SQLServer2000 table are named in Simplified Chinese (it is strongly recommended not to use Chinese as the field name). In fact, the operation is to SQL Server query records, inserted into MySQL. The scripting language of choice is php,php open mssql and MySQL extension, which is an easy issue for all two database operations.
The problem appears in SQL Server table field name is Chinese, written query statement in Sqlserve test is returned by a record, with PHP's MSSQL extension query is an error.
Surf the internet to check the information, the information on the Internet is not too much, many netizens think it is PHP's MSSQL extension does not support the SQL statement in Chinese. Check the information, PHP's MSSQL is supported in SQL with Chinese language statements. Error is mostly due to the problem of coding, encoding database and code and query statement code is not uniform, query statements to SQL Server in the Chinese part of the garbled, resulting in a query failure.

Second, the solution:

Know the reason, the next analysis and resolution, confirmation is the problem of the code is not uniform. The solution is divided into the following steps:
1, confirm the SQL Server database encoding, my data encoding is GBK.
2, confirm the current PHP script file encoding, my code is UTF-8.
3. Transform the encoding of the SQL query statement.
Add: Some netizens mentioned to the PHP script file encoding to the same as the database code, in fact, this step is not necessary, as long as you confirm that your SQL statement and database encoding is consistent, it is recommended that you do not need to convert the reason is if your php script file contains other PHP script, It also has to be all the include or require script file encoding conversion, or PHP script encoding is not uniform very error-prone, if a lot of interrelated files, this is a very troublesome problem and complicate things.

Third, the programme:

Write a conversion function that converts the SQL statement encoding before the SQL operation. My sample code is posted below:
Copy CodeThe code is as follows:
Encoding Conversion functions
function Utf8togb ($s) {
Return Iconv (' Utf-8 ', ' gbk//ignore ', $s); The IGNORE parameter is ignored when a character that is not converted is encountered
}
It is recommended to replace all Chinese characters with English aliases, so as to facilitate the following operation and encoding conversion.
$sql = "SELECT [ID], [column] as typeID, [subject] as title, [author] as author, convert (text, [body]) as body from [Article table];";
$sql = UTF8TOGB ($sql);

http://www.bkjia.com/PHPjc/326035.html www.bkjia.com true http://www.bkjia.com/PHPjc/326035.html techarticle First, the problem: The database is Ms SQLServer2000, to the SQLServer2000 in a table data import MySQL5, where the SQLServer2000 table field in Simplified Chinese name (strongly recommended not to ...

  • 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.