Re: Db_DataObject - how to force connection to be utf8?

From: Date: Fri, 12 Aug 2005 09:09:57 +0000
Subject: Re: Db_DataObject - how to force connection to be utf8?
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-20392@lists.php.net to get a copy of this message
Justin Patrin wrote:
On 8/9/05, Paul Groves <paul.groves@computing-services.oxford.ac.uk> wrote:
Hi I'm using Db_DataObject (latest version) for a project (along with PHP 5.03 and MySQl 4.1) and the database tables (which of course map to Db_DataObject objects) I'm using are all in unicode (i.e.utf8) and currently some characters (e.g. curly quotes) being read from the database fields are not being displayed correctly on the web page, though the web page is set to display utf8 (and the browser recognises this). I'm presuming this is because the database *connection* also needs to set to utf8 - normally I would just do this using an SQL command after making the connection, i.e. SET NAMES 'utf8'; However, I can't see how this is possible using DB_DataObject, since the whle database connection thing is abstracted out. I thought there might be a Db_Database configuration option to set the character set for the connection, but there doesn't appear to be one. Does anyone have any tips for how to force the connection to be utf-8 when using Db_DataObject?
Try doing this at the beginning of your application: $do = DB_DataObject::factory('tableNameHere'); $do->query('SET NAMES "utf8"');
Okay, in the end I got things to work my doing a variation on the above: /* create dummy object, so that we can access the connection * to force the enocding of the connection to UTF8 */ $obj_utf8= new DataObjects_tableNameHere(); $obj_db = &$obj_utf8->getDatabaseConnection(); // only force connection to utf8 if backend db is MySQL $str_dbtype = $obj_db->dsn['dbsyntax']; // detect both mysql and mysqli connection types if ((substr($str_dbtype,0,5)) == "mysql") { $obj_utf8->query( 'SET NAMES "utf8"' ); } // kill the dummy object unset($obj_utf8); NB: I didn't actually use "tableNameHere" as the table name, I just fairly arbitrarily picked one of the tables that's representing the objects in my project Thanks for all the help! Paul -- *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+* Paul Groves paul.groves@oucs.ox.ac.uk Senior Project Officer, Academic Computing Development Team www.oucs.ox.ac.uk/acdt/ ACDT is part of the Learning Technologies Group www.oucs.ox.ac.uk/ltg/ Oxford University Computing Services | University of Oxford | 13 Banbury Road | Oxford OX2 6NN | Tel: 01865 273290 *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*

« previous php.pear.general (#20392) next »