PHP connection to MySQL database Chinese garbled solution

Source: Internet
Author: User
Php will connect to the mysql database with garbled characters, which is generally only available in Chinese text, but not in English, because the default connection to mysql is utf8. If your page and database code are inconsistent, it is possible that

Php will connect to the mysql database with garbled characters, which is generally only available in Chinese text, but not in English, because the default connection to mysql is utf8. If your page and database code are inconsistent, garbled characters may occur.

Solution:The page is gb2312

Mysql_qurey specifies that the database is decoded as "set names gb2312" to prevent Chinese garbled characters.

The page code is uft8: mysql_query ('set NAMES utf8'); // you can SET the link encoding based on the php file encoding, for example:

$ Connection = mysql_connect ($ localhost, $ username, $ password );

Mysql_query ('set NAMES utf8'); // sets the link encoding based on the php file encoding.

Gbk

  1. Mysql_query ('set NAMES gbk ');
  2.  
  3. $ Db_selected = mysql_select_db ($ database, $ connection );
  4. If (! $ Db_selected ){
  5. Die ('Can't use db: '. mysql_error ());
  6. }

In this way, the output data will not be garbled. if the data is garbled during storage, try to make the page encoding consistent with the database field encoding.

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.