Converting encoding in mysql query

I have a question about converting encoding from an internal mysql query.

I have 2 databases. One for the site (joomla), the other for the forum (IPB). I am making a request from within joomla which has "SET NAMES UTF8" by default. I want to query a table inside the forum databases. A table called "ibf_topics". This table is encoded in latin1.

I do the following to select anything from a 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 snippet, I use the following:

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

      

The question is, is there anyway to avoid all these troubles? At the moment, I cannot change the forum database to UTF8 and I cannot change the joomla database to latin1: S

+2


a source to share


3 answers


you can open 2 database connections, 1 to joomla and one to IPB. you will use more resources, but it will make your code easier to read and you can do all the configuration (charset, etc.) in the db setup. this will also make it easier to port later if the situation changes - you don't have to go through the code deleting everything SET NAMES

.



+2


a source


$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();

$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();

      



0


a source


/ * you can set encoding after connecting to the database * /

header ('Content-Type: text / html; charset = utf-8');

/ * after connecting * /

mysql_query ("SET NAMES utf8");

/* or */

mysql_query ("SET CHARACTER SET utf8");

/* or */

mysql_query ("SET COLLATION_CONNECTION = 'utf8_general_ci'");

0


a source







All Articles