About PHP read MySQL database garbled solution, MySQL garbled _php tutorial

Source: Internet
Author: User

About PHP read MySQL database garbled solution, MySQL garbled


about PHP read MySQL database when garbled method of resolution

When PHP reads MySQL, there are several places involved in character sets.
1. Specify the character set of the database table when the database table is created. For example

  • CREATE TABLE TableName
  • (
  • ID int NOT NULL auto_increment,
  • Title varchar () NOT NULL,
  • Primary KEY (' ID ')
  • ) DEFAULT CHARSET =utf8;
    Copy Code
    2. MySQL's character set
    There are three important variables in MySQL, character_set_client,character_set_results,character_set_connection.
    By setting character_set_client, tell mysql,php what encoding is stored in the database.
    By setting Character_set_results, tell mysql,php what kind of encoded data you need to take.
    By setting character_set_connection, you tell mysql,php the text in the query and what encoding to use.
    3. After connecting to the database, set the default character encoding used when transferring characters between databases.
    Set by using Mysqli::set_charset () or Mysqli::query (' Set names UTF8 ').
    Try to use Mysqli::set_charset (mysqli:set_charset) instead of "Set NAMES"
    • $db = new mysqli (' localhost ', ' user ', ' passwd ', ' database_name ');
    • $db->set_charset (' UTF8 ');
      Copy Code
      Attention is UTF8, not utf-8.
      (The problem here is that both the database and PHP are coded together, but if you do not call the Mysqli::set_charset () function, the data will still be garbled when you read it.) Why is this? )
      (another, set names UTF8 equivalent to the following three sentences
      SET character_set_client = UTF8;
      SET character_set_results = UTF8;
      SET character_set_connection = UTF8;

      4. The character set used by the HTML page. Set in the META tag

    • Copy Code
      5. The character set used by the PHP text file.
      Under Linux, you can open a file with vim and enter
      : Set Encoding
      View the character set used by a file
      To ensure that you do not garbled, you need to ensure that the encoding of the file itself, the encoding specified in the HTML, PHP tells the MySQL code (including character_set_client and Character_set_results) unified. Use either the Mysqli:set_charset () function or the "set NAMES".
      For the question after "3", a few examples have been written to test the results of setting and not setting the character set when the linked database is tested. Test environment Ubuntu 12.04,mysql 5.5,php 5.5.7.
      The results are as follows:
      (1) The database table character set is UTF8 and does not use set names UTF8
      Ability to insert and read Chinese correctly, but garbled in MySQL
      (2) The database table character set is UTF8, using set names UTF8
      Normal insertion, reading in Chinese, MySQL display correct
      (3) database table character set is not UTF8, use set names UTF8
      As shown in MySQL, the read-out is a question mark.

      PHP Tutorial selected from 00 H5

      http://www.bkjia.com/PHPjc/1057283.html www.bkjia.com true http://www.bkjia.com/PHPjc/1057283.html techarticle about PHP read MySQL database garbled solution, MySQL garbled about PHP read MySQL database garbled solution when PHP read MySQL, there are several places related to the character ...

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.