PHP connection remote MYSQL and MYSQL5.1 Chinese garbled processing method. This article describes some problems I encountered during the development of my Blog and their solutions. Because this website uses a free remote MySql database db4free.net, and this article describes some problems I encountered during my Blog development 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 ");
Bytes. Because this website uses a free remote MySql database db4free.net, and...