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 ");