Switching Database Connections

Posted on 2009-12-16
Medium Priority
Last Modified: 2013-12-12
Hello to all experts

I have a very perplexing problem and would like to know if what I am trying to do is possible in PHP.


I have designed a website for a local Golf and Country Club and the client have requested the site to be made available in 5 different languages. Normally I would do it the old-school way and build a complete duplicate site in a separate directory using the second language and link between them  but with 5 foreign languages, this would be a colossal effort.

So what I want to achieve is to duplicate the databases in each of the languages and then load the appropriate language that the user selects from a list.

This means that I need to be able to select the language by using one of 5 connection scripts and have all page in the site use the same connection script until another language is chosen and then all the pages use the new connections script.

I just can't seem to figure out how to accomplish this and I'm beginning to wonder if can even be done in PHP. Can anyone out there confirm if this can be done?

Please see the attach flowchart to see what I mean.

Thanks to all.
Question by:SaigonJeff
  • 2
  • 2
LVL 70

Expert Comment

by:Jason C. Levine
ID: 26065397
Hi SaigonJeff,

Wouldn't it be easier to have one database (and one connection script) and just have multiple tables for the different languages?

So all you would need to do is have the user set the preference and then call the correct set of tables.  Name the tables with the language prefix (e.g. jap_content, eng_content) and then alter any calls to a table to put the prefix in dynamically:

SELECT * FROM $_SESSION['lang']_content


Author Comment

ID: 26068603
That would be easier, but the database is also feeding 5 different sites. The tables from on site are exactly duplicated for each other site and I use a prefix to distinguish the tables for each one. Adding a suffix might work better I think.

SELECT * FROM $_SESSION['table']_['lang']
LVL 70

Accepted Solution

Jason C. Levine earned 2000 total points
ID: 26068796
>> That would be easier, but the database is also feeding 5 different sites

So what?  A database can feed as many sites as you have the bandwidth to support.

>> Adding a suffix might work better I think.

Same concept...

SELECT * FROM tblName_$SESSION_['lang']

Author Closing Comment

ID: 31666869
Thanks for your help... I do believe you have given me the answer I was looking for.

I will give it a try and post my results.

In the meantime, I will accept this as a viable solution.

I always learn something here on EE.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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