Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:24:16+00:00 2026-06-08T16:24:16+00:00

I am currently using PHP PDO to access my database. That is all working

  • 0

I am currently using PHP PDO to access my database. That is all working absolutely fine and dandy. However, I am going to be adding read-replicas to my server setup, so I wish to adjust my code accordingly.

My current plan of action is to store an array of database credential details. One “read and write” set for the master MySQL database and any number of credentials for “read replicas”.

What I wish to do is add a method to the PDO class called “mode” where by a mode is passed through, such as “read” or (default) “write”. By passing this through (eg. $dbh->mode(“read”); ), it can lookup the details of a random read replica (not fussed which) and use those details for the connection. Then once i’m done reading from my replicas, do another $dbh->mode(“default”) to put it back into write mode, whereby I can use INSERT, UPDATE etc.

Can this be done without simply destroying the PDO object and creating a new one? Can connection details simply be changed after the object already exists?

So far I have the following (its barely anything, but figured its a start).

Class SwitchablePDO extends PDO
{
    public function mode($mode = "default") 
    {
        // Use the credentials for my master read and write server by default

        if($mode == "read")
        {
            // Use one the credentials for my read replicas (randomly choose)
        }

    }
}

Any help regarding this would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:24:19+00:00Added an answer on June 8, 2026 at 4:24 pm

    I would rather set-up completely different database connection objects than deal with a mode. Using a mode, you will inevitably run into a situation where a piece of code does not set the mode and relies on the previous piece of code’s mode, and will fail when called in a different context. This is known as sequential coupling.

    With multiple objects provided by a factory method, or a dependency injection container, you make sure each piece of code specifies which database connection it needs, such as master or slave.

    As a bonus, avoid using master/slave as a name and instead use names that relate to the type of tasks to be performs, like analytics, which allows you to change which server will be used without going through the code to find all related pieces of code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that
I'm currently using the following PHP code: // Get all subordinates $subords = array();
I am currently writing a CRUD class in PHP using PDO. I like the
Trying to copy a database into a new database, using a PHP script that
I'm currently using php to populate a form with selections from a database. The
I am currently using a combination of PHP, ORACLE, PDO, and JNDI to connect
I am currently using PHP/MySQL and the jQuery timeago plugin. Basically, I need to
I am currently using jQuery to load a variable php layout depending on the
I'm currently designing a web application using php, javascript, and MySQL. I'm considering two
I'm currently using Zend Framework 2 beta for on PHP 5.4.4 to develop a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.