AgainMe
AgainMe

Reputation: 780

Cannot set encoding to pdf construct

I'm trying to set UTF8 encoding to PDO construct, what I did for now is this:

public function __construct($dbType, $dbHost, $dbName, $dbUser, $dbPass, $charset)
{
    try
    {
        parent::__construct($dbType . ':host=' . $dbHost . ';dbname=' . $dbName, $dbUser,
            $dbPass. ';charset=' . $charset);
    }
    catch(PDOException $e)
    {
        $this->_error = $e->getMessage();
    }

    parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    parent::setAttribute(PDO::ATTR_PERSISTENT, true);
}

when the setAttribute line is reached I get this error:

PDO::setAttribute(): SQLSTATE[00000]: No error: PDO constructor was not called

what am I doing wrong?

Upvotes: 0

Views: 115

Answers (1)

Fran Cerezo
Fran Cerezo

Reputation: 948

Are you extending PDO? When i want to instanciate a PDO object i write this:

$pdoObj = new PDO(
            'mysql:dbname=' . DB_NAME .
            ';host=' . HOST_NAME . ";",
            USER,
            PWD,
            array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
        );

Upvotes: 1

Related Questions