Page 1 of 1

php mysql connection string characterEncoding=UTF8

Posted: Wed Jul 29, 2009 3:11 am
by markthien
Hi guys,

In java, when connecting to mysql db, I need to set like useUnicode=true&characterEncoding=UTF8 in the connection string:

Code: Select all

     Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql://mydb.com/mydb?user=mydb&password=mydb&useUnicode=true&characterEncoding=UTF8");
I just need to know how can I concate the "useUnicode=true&characterEncoding=UTF8" in php mysql connection string, which typically look like :

Code: Select all

class DBConnection {
    
    private $con = null;
    private $connection_string = 'mysql:host=localhost;dbname=mydb';
    private $username = 'mydb';
    private $password = 'mydb';
    
    function __construct() {
        try {
            $this->con = new PDO($this->connection_string, $this->username, $this->password);
            $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            error_log($e->getMessage());
        }
    }
    
    public function getConnection(){
        return $this->con;
    }
    
    function closeConnection(){
        $this->con = null;
    }
}
Appreciate any advice pls. Thanks !

regards,
Mark

Re: php mysql connection string characterEncoding=UTF8

Posted: Wed Jul 29, 2009 5:54 am
by dejvos
If you mean output encoding you have to use SQL command

Code: Select all

SET NAMES utf8;
in PHP code it seems like:

Code: Select all

mysql_query("SET NAMES utf8");

Re: php mysql connection string characterEncoding=UTF8

Posted: Wed Jul 29, 2009 6:32 am
by markthien