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 8372535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:27:23+00:00 2026-06-09T14:27:23+00:00

I am creating a helper class and adding in a PDO instance into it.

  • 0

I am creating a helper class and adding in a PDO instance into it.
I want the class method to be able to connect to various databases.
What is the best way to go around it?
So far I have:

    public static function connect($dbType, $database, $host, $username,
    $password, $options = array())
{
    switch($dbType)
    {
        case "pgsql":
            $DSN = "pgsql:dbname=$database;host=$host";
            break;

        case "mysql":
            $DSN = "mysql:host=$host;dbname=$database";
            break;

        case .....
    }
}

Is there a better way to perform this without the need to detect the $dbType over and over?
Is there a more dynamic way to do it?

  • 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-09T14:27:25+00:00Added an answer on June 9, 2026 at 2:27 pm

    Personally I don’t see the need to provision for so many databases, because PDO doesn’t abstract out the intricate query language differences and capabilities of each driver 🙂

    Having said that, you could create a function to just generate the DSN:

    function generateDSN($type, array $params)
    {
        return "$type:" . join(';', array_map(function($v, $k) {
            return "$k=$v";
        }, $params, array_keys($params)));
    }
    
    echo generateDSN('mysql', array(
            'dbname' => 'test',
            'host' => 'localhost',
            'charset' => 'utf8',
    ));
    
    mysql:dbname=test;host=localhost;charset=utf8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a static helper class for web services I'm writing. I'm tapping into
After some hours creating a vb.net helper class for unit-tests and integrating moq into
What's the best way of passing context to a custom business/helper class from an
I am in the proces of creating a helper method that will allow me
I decided to use the singleton design pattern while creating a view helper class.
I'm creating a Browser Helper Object using VS2008, C++. My class has been derived
I am creating a database helper class and for some of the methods within
I have been creating a helper class for the Facebook PHP API in order
Im creating a helper class to collect a users date of birth. I don't
I'm looking at creating a helper method to set an exception's message, automatically setting

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.