This article describes some of the problems I have encountered in developing my blog and how to solve them. Because this site is a free remote MySQL database db4free.net, and this database is 5.1 version, so there are many problems in the development process. Therefore, we will publish it here for your reference.
How to connect to a remote database
For PHP to connect to a remote MySQL database, you typically use the following statement:
The following are the referenced contents: 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); |
Second, solve the problem of garbled Chinese display
Multi-language support is introduced from MySQL 4.1, but the Chinese in PHP will appear garbled. No matter what code you use. Especially for this 5.1 version of MySQL data, he is more difficult to use in Chinese problems. The solution is as follows:
1. Set the encoding type as gb2312_chinese_ci when the table is being built.
2, in the PHP page of the database connection statement plus a line mysql_query ("SET NAMES ' gb2312 '", $link); For example
The following are the referenced contents: $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); |
So the Chinese in MySQL will be able to display the normal. You may also use the following sentence:
mysql_query ("SET NAMES ' gb2312 '");