SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    set_charset and PDO ?

    Hello

    I have (almost) decided to change from Mysqli to PDO. But have run into one problem: charset.

    In Mysqli I used
    PHP Code:
    set_charset('utf8'
    (in the db connection) to be sure that swedish letter was saved properly in the mysql database.

    But using PDO to connect something is wrong. And I can't find any similar to use with PDO.

    The php-page have charset utf-8 in the header, Mysql is set to UTF-8 Unicode_ci (localhost), the db and tables have collation set to utf8_unicode_ci.

    This is a common issue for me everytime But how to solve it with PDO. Mysqli and set_charset('utf8') worked like a dream

    Regards, Magnus

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try putting this in the PDO instantiation string:
    Code:
    charset=UTF-8
    Inside the DSN string. I'm not sure if it'll work, but you can give it a try.
    This is a common issue for me everytime
    I've never experienced such a problem, and I'm using PDO all the time. Is there actually a charset problem or are you taking precautions?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I've never experienced such a problem, and I'm using PDO all the time. Is there actually a charset problem
    I think so

    Using PDO connection to insert into db gives wrong character (checked with phpmyadmin - served as UTF-8). Using Mysqli and set_charset works fine (had the same problem first before added the set_charset).

    Well, I tried your idea. Added it at the end of the DNS string. But no, same problem

    Regards, Magnus

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SET NAMES utf8;
    SET CHARACTER SET utf8;
    More: Connection Character Sets and Collations.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Gulp, lets hope there is an easy way round this.

    Kore Nordman in his faq says:

    You can also set the connection charset only for one connection, by sending the following query before you send data to the database server:

    SET NAMES utf8
    Last edited by Cups; Feb 19, 2009 at 11:16. Reason: Bah, had me again decowski - i dont need to follow you on Twitter ... I follow you on SP

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everybody You are truly experts.

    I added this line
    PHP Code:
    $pdoConnectLink->query('SET NAMES utf8'); 
    in the method where I create the PDO connection. Ended up like this:
    PHP Code:
    $pdoConnectLink = new PDO($this->_dbstring$this->_username$this->_password);
    $pdoConnectLink->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    $pdoConnectLink->query('SET NAMES utf8');
    return 
    $pdoConnectLink


    Your sincerely Magnus

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was having the same issue. Thanks for the solution.


    Btw, if anyone have a class, this could be usefull:
    PHP Code:
    $db = new PDO('mysql:host=localhost; port=3306; dbname=something''root''', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 
    or this:
    PHP Code:
    private $arrayopt
    +
    PHP Code:
    $this->arrayopt= array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); 
    +
    PHP Code:
    parent::__construct($this->dsn$this->username$this->password$this->arrayopt); 

    Regards,
    Márcio


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •