Re: DataObject, mysql 4.1, unicode
| From: | Justin Patrin | Date: | Tue, 18 Jan 2005 18:20:12 +0000 |
| Subject: | Re: DataObject, mysql 4.1, unicode | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-16998@lists.php.net to get a copy of this message | ||
On Tue, 18 Jan 2005 12:05:59 +0000, JEnsign <justjohn@gmail.com> wrote:
> Hi all,
>
> I am using php5.0.3 and mysql 4.1, and the latest DataObjects package.
>
> The entire db is unicode general and I am using the mysqli driver.
>
> If i do not expliclity call a "set name 'utf8' " query before a find(),
> all i get back is a bunch of question marks....
>
> By example:
>
> $DO_trans = DB_DataObject::factory('translations');
> $DO_trans->query("SELECT * FROM {$DO_trans->__table} ORDER BY
> fk_localeid, section");
>
> I get a bunch of ???? marks for various value fields. ( remember all
> collations are unicode_general).
>
> IF i do this:
>
> $DO_trans = DB_DataObject::factory('translations');
>
> $DO_trans->query("SET NAMES 'utf8'"); <-----added this
> line
>
> $DO_trans->query("SELECT * FROM {$DO_trans->__table} ORDER BY
> fk_localeid, section");
>
> Then all my results are indeed as expected, with all characters
> including, chinese, and japanese characters coming back as they should.
>
> THERE has got to be a better way! I am using DataObjects on a year
> long project. I will end up writing $DO_[some name here]->query("SET
> NAMES 'utf8'"); a million times before its over! Not too mention
> running 2 querries for everyone 1 that i needed.
>
> Do you know if:
>
> 1- I can simply add an option to the dsn like:
> mysqli://user:password@localhost/database?character_set_connection=utf8
> ? (this doesnt work btw)
> 2- Can I hack the DataObject file to always call a SET NAMES 'utf8'
> query before running the "find()" method? ( a ton of extra querries for
> something that should be automated, least favorite idea )
> 3- Some magical setting for mysqli somewhere? In an ini file for the
> mysql or php?
>
> in a jam, and thanks for your time,
>
You can always make a class which extends DB_DataObject that overrides
the _connect method and runs this query after parent::_connect, but
this will cause a *lot* of extra calls for you. I don't suggest this
one.
Actually, your best bet us probably to make your own DB driver,
mysqliutf8 or some such. Change your DSN to use that, then create the
driver and put it in the DB folder.
require_once('DB/mysqli.php');
class DB_mysqliutf8 extends DB_mysqli {
function connect ($dsninfo, $persisten = false) {
$ret = parent::connect($dsninfo, $persistent);
if (PEAR::isError($ret)) {
return $ret;
}
$this->query('SET NAMES "utf8"');
return DB_OK;
}
}
This way, it's called automatically *only* when you connect.
--
Justin Patrin