Solutions to garbled characters when php reads and writes mysql data

Source: Internet
Author: User
Solutions to garbled characters when php reads and writes mysql data
When php reads and writes mysql data, garbled characters are usually caused by inconsistent database encoding, database table encoding, database table field encoding, and PHP output page encoding. You can refer to the solution described in this article.

This article uses UTF-8 encoding example: 1. First, make sure that the mysql connection statement contains:

mysql_query(“SET NAMES ‘utf8′”);mysql_query(“SET CHARACTER_SET_CLIENT=utf8″);mysql_query(“SET CHARACTER_SET_RESULTS=utf8″);

The complete connection code is as follows:

$host=”localhost”;$user=”root”;$pwd=”root”;$dbname=”wordpress”;$link = mysql_connect($host, $user, $pwd)or die(“Could not connect: ” . mysql_error());mysql_select_db($dbname, $link) or die (‘Can’t use wordpress : ‘ . mysql_error());mysql_query(“SET NAMES ‘utf8′”);mysql_query(“SET CHARACTER_SET_CLIENT=utf8″);mysql_query(“SET CHARACTER_SET_RESULTS=utf8″);

2. check whether the database encoding, database table encoding, database field encoding, and page encoding are consistent.

3, if there is still garbled, please check the php file encoding, you can save the file, when saving select UTF-8 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.