PHP uses PDO to operate the database garbled problem solution, pdo garbled_PHP tutorial

WBOY
Release: 2016-07-12 08:54:25
Original
1157 people have browsed it

The solution to the garbled code problem when PHP uses PDO to operate the database, pdo garbled code

This article describes the example of the solution to the garbled code problem when PHP uses PDO to operate the database. Share it with everyone for your reference, the details are as follows:

When using PDO connection to operate the database, sometimes it will appear: the Chinese characters saved in the database are garbled. If the file is in UTF-8 format, the solution is as follows:

(1) The instantiated object directly executes the query() method or exec() method:

<&#63;php
  class DB {
    static public function getDB() {
      try {
        $_opts_values = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2);
        $_pdo = new PDO(DB_DSN, DB_NAME, DB_PASS, $_opts_values);
      } catch (PDOException $e) {
        exit('数据库连接错误!错误信息:'.$e->getMessage());
      }
      $_pdo->query("SET NAMES utf8"); // $_pdo->exec('SET NAMES utf8'); //设置数据库编码,两种方法都可以
      return $_pdo;
    }
  }
&#63;>

Copy after login

(2) Add the MYSQL_ATTR_INIT_COMMAND attribute to the fourth parameter of the instantiated PDO:

<&#63;php
  class DB {
    static public function getDB() {
      try {
        $_opts_values = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
        $_pdo = new PDO(DB_DSN, DB_NAME, DB_PASS, $_opts_values);
      } catch (PDOException $e) {
        exit('数据库连接错误!错误信息:'.$e->getMessage());
      }
      return $_pdo;
    }
  }
&#63;>

Copy after login

Note: The above methods have been tested.

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP database operation skills based on pdo", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "PHP basic Grammar introductory tutorial", "php office document operation skills summary (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) Usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Some supplements to solve the Chinese garbled problem with PDO in PHP
  • php PDO Chinese garbled solution
  • PHP PDO transaction processing analysis
  • PHP PDO common class library example analysis
  • PHP PDO operation simple example
  • Simple usage of PDO in PHP5.2
  • The PDO method in php realizes the addition, deletion, modification and query of the database
  • Detailed explanation of the use of PDO, the mysql connection method in php
  • Comparative analysis of the database connection method pdo and mysqli in php
  • PHP PDO List of output results of various parameters of fetch mode
  • Detailed explanation of PDO method using PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119985.htmlTechArticleSolution to the garbled code problem when PHP uses PDO to operate the database. pdo garbled code This article tells an example of the garbled code problem when PHP uses PDO to operate the database. Problem solving. Share it with everyone for your reference, specifically...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template