Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:00:18+00:00 2026-06-16T10:00:18+00:00

According to php.net, Stack Overflow and other sources of trust, I can find 4

  • 0

According to php.net, Stack Overflow and other sources of trust, I can find 4 different ways to set UTF-8 on PDO connection, but can’t find which one is the better to choose:

$pdo_db = 'mysql:host=localhost;dbname=local_db;charset=utf8'; // METHOD #1
$pdo_login = 'root';
$pdo_pass = 'localpass';

$db = new PDO($pdo_db, $pdo_login, $pdo_pass, array(
    PDO::ATTR_ERRMODE => $localhost ? PDO::ERRMODE_EXCEPTION : PDO::ERRMODE_SILENT,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', // METHOD #2
));
$db->exec('SET NAMES utf8'); // METHOD #3
$db->exec('SET CHARACTER SET utf8'); // METHOD #4

So, what I understood, is that method 1 only works with PHP 5.3+ (but it seems that it’s a bit buggy), and method 2 is for MySQL only.
Differences between method 3 and 4 is MySQL thing, but I still don’t know which one is better. And is there a way to call SET NAMES in PDO attributes, but not for MySQL only?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:00:20+00:00Added an answer on June 16, 2026 at 10:00 am

    Setting it in DSN is the only proper way (although it is only supported since 5.3).
    You can this one and SET NAMES at the same time.

    All the other ways will make infamous half-fictional GBK injection possible.

    Please note that your setting for error_reporting() is utterly wrong. it have to be unconditional -1.
    If you concerned about displaying errors – there is a proper ini setting for this, called display_errors, can be set at runtime.
    While error_reporting sets level of the error and should be at max all the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the PDO documentation , you can store database login details in php.ini,
Possible Duplicate: In PHP can someone explain cloning vs pointer reference? According to http://php.net/manual/en/language.oop5.references.php
According to PHP.net manual, pg_pconnect will create a persistent connection, or will return the
According to one of the man pages http://www.php.net/manual/en/language.oop5.static.php : Calling non-static methods statically generates
According to: http://www.php.net/manual/en/function.filectime.php In most Unix filesystems, a file is considered changed when its
According to these posts in the php.net manual it should be possible to supply
According to http://php.net/manual/en/functions.user-defined.php , I should be able to use an underscore (_) as
according to php.net date('m') should should 01 but i'm getting 1 instead dirloc =
According to php.net I should use mysql_real_escape_string() and turn off magic quotes , because
According to the comment in PHP manual about Copy(): http://php.net/manual/en/function.copy.php The copy() will change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.