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 is why SyntaxHighlighter. all ();

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.