PHP MSSQL Extended SQL query text segment name resolution method

Source: Internet
Author: User
Tags sql server query mssql php script


First, the question:

The database is Ms SQLServer2000, to the SQLServer2000 of a table of data into the MySQL5, where SQLServer2000 table fields in Simplified Chinese name (strongly recommended not to do in Chinese as field name). In fact, the operation is to SQL Server query records, inserted into MySQL. The selected scripting language is php,php open MSSQL and MySQL extensions, which are easy issues for all two database operations.

The problem appears in SQL Server the table field name is Chinese, the written query statement in the Sqlserve test is returned through a record, using PHP's MSSQL extended query is an error.

Internet access to information, the relevant information on the Internet is not too much, many netizens believe that the MSSQL extension of PHP does not support SQL statements in Chinese. Check the data, PHP's MSSQL is to support SQL in the Chinese language statement. Most of the problem is because of coding problems, coding database and Coding and query statement coding is not uniform, query statements to SQL Server in the Chinese part of the garbled, resulting in query failure.

Second, the solution:

Know the reason, and then analyze the solution, confirm that the coding is not uniform problem. The solution is divided into the following steps:

1. Confirm the code of SQL Server database, my data coding is GBK.

2, confirm the current PHP script file encoding, my code is UTF-8.

3, the conversion of SQL query statements of the code.

Add: Some netizens mentioned to the PHP script file encoding and database coding consistent, in fact, this step is not necessary, as long as you confirm your SQL statements and database coding consistent can be, here is recommended not to convert the reason is if your php script file contains other PHP script, That also has to all include or require script file encoding conversion, otherwise the PHP script coding is not unified is very easy to make mistakes, if the correlation file is many, this is also a very troublesome question and complicates the matter.

Iii. programme:

Write a transformation function to encode the SQL statements before the SQL operation. My sample code is posted below:

Encoding conversion function

  code is as follows copy code

function UTF8TOGB ($s) {

return iconv (' Utf-8 ', ' gbk//ignore ', $s); //IGNORE parameter is ignored when no converted characters are encountered

}

// It is suggested that all Chinese characters be replaced with English aliases, which is convenient for the following operation and coding conversion.

$sql = "SELECT [ID], [column] as typeid, [topic] as title, [author] as author, convert (text, [positive Text] as body from [Article 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.