I have an admin-section in my webapp where you can write blogposts, however, I want to be able to encode the text im writing with a php-function before saving it into the database.
I can’t controle the database because it’s not mine, and it has trouble with the encoding. So everything that goes in and out of the database needs to be encoded with the function below.
yeah, im using MSSQL 2000, feels like I have tried everything to get the encoding correct. Even had help from a friend who have a lot of experience with MSSQL.
So I gave up and now im using that function everywhere
Do you know how I can use this part of the “set up unicode” guide? (I don’t know where to put the code)
SET NAMES utf8 ;
Such a command can be put in the initSQLs attribute of the db component. The charset attribute introduced above should be sufficient, though.
**
* Initializes the open db connection.
* This method is invoked right after the db connection is established.
* The default implementation is to set the charset for MySQL and PostgreSQL database connections.
* @param PDO $pdo the PDO instance
*/
protected function initConnection($pdo)
{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($this->emulatePrepare!==null && constant('PDO::ATTR_EMULATE_PREPARES'))
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,$this->emulatePrepare);
if($this->charset!==null)
{
$driver=strtolower($pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
if(in_array($driver,array('pgsql','mysql','mysqli')))
$pdo->exec('SET NAMES '.$pdo->quote($this->charset));
}
if($this->initSQLs!==null)
{
foreach($this->initSQLs as $sql)
$pdo->exec($sql);
}
}
As you can see, SET NAMES is executed only for Postgres and Mysql.
I am sure you can set the encoding for your database server in case you want to know how you can perform that function before all your save calls you can use Active record hooks