Solved

Multi connect to a single database with different charset(s) via PHP

Posted on 2007-04-02
2
305 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:Claudia_Presto
2 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18843055
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.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 18843590
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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now