PHP connection remote MySQL and MYSQL5.1 Chinese garbled processing Method _php Tutorial

Source: Internet
Author: User

This article describes some of the problems I have encountered in developing my blog and how to solve them. Because this site uses 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 for your reference.

I. How to connect a remote database

For PHP to connect to a remote MySQL database, you typically use the following statement:

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 Chinese display garbled

Since MySQL 4.1 introduced multi-language support, but the Chinese inserted in PHP will appear garbled. No matter what code you use. Especially for this 5.1 version of MySQL data, he is more troublesome to use in the Chinese language problem. The workaround is as follows:

1, in the construction of the table when the encoding type is gb2312_chinese_ci.

2, in the PHP page database connection statement plus a line mysql_query ("SET NAMES gb2312", $link); 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 in MySQL can be displayed normally. You can also use the following sentence:

mysql_query ("SET NAMES gb2312");


http://www.bkjia.com/PHPjc/531673.html www.bkjia.com true http://www.bkjia.com/PHPjc/531673.html techarticle This article describes some of the problems I have encountered in developing my blog and how to solve them. Because this site uses a free remote MySQL database db4free.net, and ...

  • 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.