This article describes some of the problems I encountered during the development of my blog and how to solve them. Because this website uses a free remote MySql database db4free.net, and this database is version 5.1, many problems occurred during the development process. Therefore, it is published here for everyone’s reference.
1. Method of connecting to remote database
For PHP to connect to a remote MySql database, the following statement is usually used:
var $serverName = db4free.net:3306;//Database server
var $dbName = dbname;//Database name
var $dbUsername = username;//Username
var $dbPassword = 123;//Login password
mysql_connect($serverName,$dbUsername,$dbPassword);
mysql_select_db($dbName);
2. Solve the problem of Chinese displaying garbled characters
Multi-language support has been introduced since MySQL 4.1, but Chinese characters inserted using PHP will appear garbled. No matter what encoding is used, it will not work. Especially for this 5.1 version of MySql data, it is more troublesome to use it in Chinese. The solution is as follows:
1. When creating the table, set the encoding type to gb2312_chinese_ci.
2. Add a line 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);
This way the Chinese in MYSQL can be displayed normally. You can also use the following sentence:
mysql_query("SET NAMES gb2312");