Convert charset in mysql query

Posted by Yousf on Stack Overflow See other posts from Stack Overflow or by Yousf
Published on 2010-04-23T22:46:32Z Indexed on 2010/04/23 22:53 UTC
Read the original article Hit count: 197

Filed under:
|
|
|
|

Hi,

I have a question about converting charset from inside mysql query.

I have a 2 databases. One for the website (joomla), the other for forum (IPB). I am doing query from inside joomla, which by default have "SET NAMES UTF8". I want to query a table inside the forum databases. A table called "ibf_topics". This table has latin1 encoding.

I do the following to select anything from the not-utf8 table.

//convert connection to handle latin1.
$query = "SET NAMES latin1";
$db->setQuery($query);
$db->query();

$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();
//read result into an array.

//return connection to handle UTF8.
$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();

After that, when I want to use the selected tile, I use the following:

echo iconv("CP1256", "UTF-8", $topic['title'])

The question is, is there anyway to avoid all this hassle? For now, I can't change forum database to UTF8 and I can't change joomla database to latin1 :S

© Stack Overflow or respective owner

Related posts about mysql

Related posts about charset