Switching Database Connections

Posted on 2009-12-16
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
    LVL 70

    Expert Comment

    by:Jason C. Levine
    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

    LVL 5

    Author Comment

    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

    >> 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']
    LVL 5

    Author Closing Comment

    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

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    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 …

    732 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

    18 Experts available now in Live!

    Get 1:1 Help Now