PHP connection remote MYSQL and MYSQL5.1 Chinese garbled Processing Method

Source: Internet
Author: User

This article describes some problems I encountered during the development of my Blog and their solutions. Because the website uses a free remote MySql database db4free.net and the database version is 5.1, many problems occur during development. This article is for your reference.

I. Methods for connecting to a remote database

The following statement is usually used to connect PHP to a remote MySql database:

Var $ serverName = db4free.net: 3306; // Database Server
Var $ dbName = dbname; // Database Name
Var $ dbUsername = username; // User Name
Var $ dbPassword = 123; // Login Password
Mysql_connect ($ serverName, $ dbUsername, $ dbPassword );
Mysql_select_db ($ dbName );

Ii. Solve the Problem of garbled Chinese Characters

Multi-language support was introduced from MySQL 4.1, but garbled characters may appear in the Chinese characters inserted using PHP. Especially for MySql Data of this version 5.1, It is troublesome to use Chinese characters. The solution is as follows:

1. Set the encoding type to gb2312_chinese_ci when creating a table.

2. Add a mysql_query ("set names gb2312", $ link) to the database connection Statement on the PHP page. For example:

$ Db_host = "localhost ";
$ Db_user = "root ";
$ Db_password = "password ";
$ Db_name = "test ";
$ Link = mysql_connect ($ db_host, $ db_user, $ db_password );
Mysql_query ("set names gb2312", $ link );
$ Db = mysql_select_db ($ db_name, $ link );

In this way, the Chinese characters in MYSQL can be displayed normally. You can also use the following sentence:
 
Mysql_query ("set names gb2312 ");


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.