PHP connection remote MySQL and MYSQL5.1 Chinese garbled processing method

Source: Internet
Author: User
Tags connect mysql mysql database

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



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.