PHP using PDO operation database garbled problem solving method, PDO garbled _php tutorial

Source: Internet
Author: User
Tags getmessage php database

PHP using PDO operation database garbled problem solving method, PDO garbled


This paper explains the problem solving method of PHP using PDO operation database. Share to everyone for your reference, as follows:

When using the PDO connection to manipulate the database, sometimes it appears: the Chinese characters in the database are garbled. The file is in UTF-8 format with the following workaround:

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

<?php  class DB {    static public function Getdb () {      try {        $_opts_values = array (pdo::attr_persistent=& GT;TRUE,PDO::ATTR_ERRMODE=>2);        $_pdo = new PDO (DB_DSN, Db_name, Db_pass, $_opts_values);      } catch (Pdoexception $e) {        exit (' Database connection Error! Error message: '. $e->getmessage ());      }      $_pdo->query ("SET NAMES UTF8"); $_pdo->exec (' SET NAMES utf8 '); Set the database encoding, both methods can      return $_pdo;    }  ? >

(2) Add the Mysql_attr_init_command attribute in the fourth parameter of the instanced PDO:

<?php  class DB {    static public function Getdb () {      try {        $_opts_values = array (pdo::attr_persistent=& Gt;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 (' Database connection Error! Error message: '. $e->getmessage ());      }      return $_pdo;    }  ? >

Note: The above methods have been tested.

More interested in PHP related content readers can view the topic: "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Tips", "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document Tips Summary ( including word,excel,access,ppt), PHP date and Time usage summary, PHP Primer for object-oriented programming, PHP string usage Summary, PHP+MYSQL database Operations Starter Tutorial, and A summary of common PHP database operation techniques

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Some additions to the problem of solving Chinese garbled in the PDO in PHP
    • PHP PDO Chinese garbled solution
    • Analysis of the transaction processing of PDO in PHP
    • Analysis of Common library examples of PDO in PHP
    • Simple example of the PDO operation in PHP
    • A simple way to use PDO in PHP5.2
    • The method of PDO in PHP to realize the deletion and modification of database
    • MySQL connection in PHP using PDO details
    • Database connection in PHP comparison of PDO and mysqli
    • List of output results of various parameters of PHP PDO fetch mode
    • PHP using the PDO method

http://www.bkjia.com/PHPjc/1119985.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119985.html techarticle PHP using PDO to manipulate the database garbled problem solving method, PDO garbled This article describes the PHP using the PDO operation database garbled problem solving method. Share to everyone for your reference, specific ...

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