Multi connect to a single database with different charset(s) via PHP
I tried to multi connect to a Mysql database with two different charset via PHP. but it seems it's not possible. because when i set charset of the second connection it will override charset of first connection!
#--(Begin)-->connecting to DB
$sourceDbInfo['link']=mysql_connect($sourceDbInfo['host'], $sourceDbInfo['username'], $sourceDbInfo['password']);
mysql_select_db($sourceDbInfo['name'],$sourceDbInfo['link']);
if ($sourceDbInfo['encoding']=='utf8') {
mysql_query("SET NAMES utf8",$sourceDbInfo['link']);
mysql_query("SET CHARACTER SET utf8",$sourceDbInfo['link']);
}
$destinationDbInfo['link']=mysql_connect($destinationDbInfo['host'], $destinationDbInfo['username'], $destinationDbInfo['password']);
mysql_select_db($destinationDbInfo['name'],$destinationDbInfo['link']);
if ($destinationDbInfo['encoding']=='utf8') {
mysql_query("SET NAMES utf8", $destinationDbInfo['link']);
mysql_query("SET CHARACTER SET utf8", $destinationDbInfo['link']);
}
#--(End)-->connecting to DB
This is correct: Only one character set is available to a single user at a time. You have two options:
1. Close the first connection before opening the second with the new charset.
2. Use different login information (username and password). MySQL will then think you are a different user, and *should* allow the use of a different charset.
use (set the fourth paramter of mysql_connect to true to create a new link, even if the same host, user and password is used): http://de3.php.net/mysql_connect
#--(Begin)-->connecting to DB
$sourceDbInfo['link']=mysql_connect($sourceDbInfo['host'], $sourceDbInfo['username'], $sourceDbInfo['password'],true);
mysql_select_db($sourceDbInfo['name'],$sourceDbInfo['link']);
if ($sourceDbInfo['encoding']=='utf8') {
mysql_query("SET NAMES utf8",$sourceDbInfo['link']);
mysql_query("SET CHARACTER SET utf8",$sourceDbInfo['link']);
}
$destinationDbInfo['link']=mysql_connect($destinationDbInfo['host'], $destinationDbInfo['username'], $destinationDbInfo['password'], true);
mysql_select_db($destinationDbInfo['name'],$destinationDbInfo['link']);
if ($destinationDbInfo['encoding']=='utf8') {
mysql_query("SET NAMES utf8", $destinationDbInfo['link']);
mysql_query("SET CHARACTER SET utf8", $destinationDbInfo['link']);
}
#--(End)-->connecting to DB
Authored by: Claudia_Presto
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
1. Close the first connection before opening the second with the new charset.
2. Use different login information (username and password). MySQL will then think you are a different user, and *should* allow the use of a different charset.