Experience exchange from GB to utf-8_

Source: Internet
Author: User
Tags dsn mysql manual sprintf
Now a lot of Chinese websites are encoded in from gb2312 to Utf-8 code conversion. There are a number of problems here to summarize:

Premise:

Ensure that all files are UTF-8 encoded, not ANSI, when they are saved

Settings: If you are using a text editor, there is an encoding option when you save the file (Figure 1) and select it as UTF-8.

If you're using Dreamweaver then it's a bit of a hassle.

When you use Dreamweaver to edit a static file, modify the file's CharSet label to Utf-8: change to and then choose Save as this time dream will save the file as Utf-8 encoded

Note: If you are using an Access database, you only need the above two steps, and access itself uses UTF-8 encoding.

ASP article

code page settings:

The first line of the ASP file: <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>Change to: <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>It also directly determines the encoding that ASP uses to access the database through ADO.
Note: If you are using an Access database, you only need the above two steps, and access itself uses UTF-8 encoding.

If you use a template program

The usual template program, through the FSO object to read and write files. But this is not a way to support utf-8, you need to change to a stream object to read and write files, here are a few lines of key code:
Read files using Stram
Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "utf-8"
.Position = objStream.Size
.LoadFromFile server.mappath("sc.htm")
wstr = .ReadText
.Close
End With
Set objStream = Nothing
Write a file using stream Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "utf-8"
.Position = objStream.Size
.WriteText=wstr
.SaveToFile server.mappath("wz/sc_" & classid & ".htm"),2
.Close
End With
Set objStream = Nothing

PHP article

PHP Connects to access:

COM extensions that use PHP on Windows hosts can connect to an Access database and specify a code page parameter of 65001 when establishing adodb.connection

$conn = new COM("ADODB.Connection",NULL,65001) or die("ADO connect failed!");
$rs = new COM("ADODB.RecordSet") or die("ADO recordset fail"); $conn->open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb");
$rs = $conn->Execute("select * from data");
while not $rs->EOF
echo $rs->Fields['title']->value;
?>

PHP connection with MySQL

After establishing a connection to the MySQL database, execute the following SQL query first:

MySQL manual about set names set names shows what character set is used in the SQL statement sent by the client. Therefore, the set NAMES ' cp1251 ' statement tells the server that "the information coming from this client will be in character set cp1251". It also specifies a character set for the result that the server sends back to the client. (For example, if you use a SELECT statement, it indicates what character set the column values use.) )
The SET NAMES ' x ' statement is equivalent to these three statements:

mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET character_set_connection = x;
Setting X to Character_set_connection also sets Collation_connection as the default proofing rule for X.

Use the ADODB class:

Set the ' Charpage ' property to 65001;

require_once('adodb/adodb.inc.php');
$conn =& ADONewConnection('ado_access');
$conn->charPage ='65001';
//$conn->charPage = 65001;
//$conn->debug = true;
$dsn = sprintf("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= %s",realpath('2data.mdb'));
// $dsn = sprintf("Driver={Microsoft Access Driver (*.mdb)};Dbq=%s",realpath("data.mdb"));
$conn->Connect($dsn);
  • Related Article

    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.