OOP and connecting to multiple databases using PDO











up vote
2
down vote

favorite












I'm just starting to learn OOP and I've setup a class to handle the database connections and wanted to check if I'm going about this the right way.



My website is already using PDO for prepared statements but it's procedural code otherwise. The site connects to multiple databases and one of the databases has two connections for separate privileges.



I have created a Dsn class which contains a method for each connection. My config file instantiates the class and opens all the connections using the methods and passing params where necessary. I'll put the code below but just wanted to check if connecting to multiple databases in this way through a single class follows best practices for OOP.



config.php



$dbh = new Dsn;
$pdo = $dbh->connnect1Read(NAME1, USER2, PASS2);
$pdo2 = $dbh->connnect2Write(NAME1, USER1, PASS1);
$pdo3 = $dbh->connnect3(USER2, PASS2);
$pdo4 = $dbh->connnect4(USER3, PASS3);
$pdo5 = $dbh->connnect5(USER4, PASS4);


dsn.php



    class Dsn
{

private $host = 'localhost';
private $charset = 'utf8mb4';

private $dbName1;
private $userName1;
private $userPass1;

private $dbName2 = 'db2';
private $userName2;
private $userPass2;

private $dbName3 = 'db3';
private $userName3;
private $userPass3;

private $dbName4 = 'db4';
private $userName4;
private $userPass4;


private $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];


public function connnect1Read($dbName, $userName, $userPass)
{
$this->dbName1 = $dbName;
$this->userName2 = $userName;
$this->userPass2 = $userPass;

$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
$pdo1 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

return $pdo1;
}

public function connnect2Write($dbName1, $userName, $userPass)
{
$this->dbName1 = $dbName1;
$this->userName1 = $userName;
$this->userPass1 = $userPass;

$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
$pdo2 = new PDO($dsn, $this->userName1, $this->userPass1, $this->opt);

return $pdo2;
}

public function connnect3($userName, $userPass)
{
$this->userName2 = $userName;
$this->userPass2 = $userPass;

$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName2 . ";charset=" . $this->charset . "";
$pdo4 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

return $pdo3;
}

public function connnect4($userName, $userPass)
{
$this->userName3 = $userName;
$this->userPass3 = $userPass;

$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName3 . ";charset=" . $this->charset . "";
$pdo5 = new PDO($dsn, $this->userName3, $this->userPass3, $this->opt);

return $pdo4;
}

public function connnect5($userName, $userPass)
{
$this->userName4 = $userName;
$this->userPass4 = $userPass;

$dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName4 . ";charset=" . $this->charset . "";
$pdo3 = new PDO($dsn, $this->userName4, $this->userPass4, $this->opt);

return $pdo5;
}
}









share|improve this question







New contributor




Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    I'm just starting to learn OOP and I've setup a class to handle the database connections and wanted to check if I'm going about this the right way.



    My website is already using PDO for prepared statements but it's procedural code otherwise. The site connects to multiple databases and one of the databases has two connections for separate privileges.



    I have created a Dsn class which contains a method for each connection. My config file instantiates the class and opens all the connections using the methods and passing params where necessary. I'll put the code below but just wanted to check if connecting to multiple databases in this way through a single class follows best practices for OOP.



    config.php



    $dbh = new Dsn;
    $pdo = $dbh->connnect1Read(NAME1, USER2, PASS2);
    $pdo2 = $dbh->connnect2Write(NAME1, USER1, PASS1);
    $pdo3 = $dbh->connnect3(USER2, PASS2);
    $pdo4 = $dbh->connnect4(USER3, PASS3);
    $pdo5 = $dbh->connnect5(USER4, PASS4);


    dsn.php



        class Dsn
    {

    private $host = 'localhost';
    private $charset = 'utf8mb4';

    private $dbName1;
    private $userName1;
    private $userPass1;

    private $dbName2 = 'db2';
    private $userName2;
    private $userPass2;

    private $dbName3 = 'db3';
    private $userName3;
    private $userPass3;

    private $dbName4 = 'db4';
    private $userName4;
    private $userPass4;


    private $opt = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
    ];


    public function connnect1Read($dbName, $userName, $userPass)
    {
    $this->dbName1 = $dbName;
    $this->userName2 = $userName;
    $this->userPass2 = $userPass;

    $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
    $pdo1 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

    return $pdo1;
    }

    public function connnect2Write($dbName1, $userName, $userPass)
    {
    $this->dbName1 = $dbName1;
    $this->userName1 = $userName;
    $this->userPass1 = $userPass;

    $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
    $pdo2 = new PDO($dsn, $this->userName1, $this->userPass1, $this->opt);

    return $pdo2;
    }

    public function connnect3($userName, $userPass)
    {
    $this->userName2 = $userName;
    $this->userPass2 = $userPass;

    $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName2 . ";charset=" . $this->charset . "";
    $pdo4 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

    return $pdo3;
    }

    public function connnect4($userName, $userPass)
    {
    $this->userName3 = $userName;
    $this->userPass3 = $userPass;

    $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName3 . ";charset=" . $this->charset . "";
    $pdo5 = new PDO($dsn, $this->userName3, $this->userPass3, $this->opt);

    return $pdo4;
    }

    public function connnect5($userName, $userPass)
    {
    $this->userName4 = $userName;
    $this->userPass4 = $userPass;

    $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName4 . ";charset=" . $this->charset . "";
    $pdo3 = new PDO($dsn, $this->userName4, $this->userPass4, $this->opt);

    return $pdo5;
    }
    }









    share|improve this question







    New contributor




    Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm just starting to learn OOP and I've setup a class to handle the database connections and wanted to check if I'm going about this the right way.



      My website is already using PDO for prepared statements but it's procedural code otherwise. The site connects to multiple databases and one of the databases has two connections for separate privileges.



      I have created a Dsn class which contains a method for each connection. My config file instantiates the class and opens all the connections using the methods and passing params where necessary. I'll put the code below but just wanted to check if connecting to multiple databases in this way through a single class follows best practices for OOP.



      config.php



      $dbh = new Dsn;
      $pdo = $dbh->connnect1Read(NAME1, USER2, PASS2);
      $pdo2 = $dbh->connnect2Write(NAME1, USER1, PASS1);
      $pdo3 = $dbh->connnect3(USER2, PASS2);
      $pdo4 = $dbh->connnect4(USER3, PASS3);
      $pdo5 = $dbh->connnect5(USER4, PASS4);


      dsn.php



          class Dsn
      {

      private $host = 'localhost';
      private $charset = 'utf8mb4';

      private $dbName1;
      private $userName1;
      private $userPass1;

      private $dbName2 = 'db2';
      private $userName2;
      private $userPass2;

      private $dbName3 = 'db3';
      private $userName3;
      private $userPass3;

      private $dbName4 = 'db4';
      private $userName4;
      private $userPass4;


      private $opt = [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES => false,
      ];


      public function connnect1Read($dbName, $userName, $userPass)
      {
      $this->dbName1 = $dbName;
      $this->userName2 = $userName;
      $this->userPass2 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
      $pdo1 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

      return $pdo1;
      }

      public function connnect2Write($dbName1, $userName, $userPass)
      {
      $this->dbName1 = $dbName1;
      $this->userName1 = $userName;
      $this->userPass1 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
      $pdo2 = new PDO($dsn, $this->userName1, $this->userPass1, $this->opt);

      return $pdo2;
      }

      public function connnect3($userName, $userPass)
      {
      $this->userName2 = $userName;
      $this->userPass2 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName2 . ";charset=" . $this->charset . "";
      $pdo4 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

      return $pdo3;
      }

      public function connnect4($userName, $userPass)
      {
      $this->userName3 = $userName;
      $this->userPass3 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName3 . ";charset=" . $this->charset . "";
      $pdo5 = new PDO($dsn, $this->userName3, $this->userPass3, $this->opt);

      return $pdo4;
      }

      public function connnect5($userName, $userPass)
      {
      $this->userName4 = $userName;
      $this->userPass4 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName4 . ";charset=" . $this->charset . "";
      $pdo3 = new PDO($dsn, $this->userName4, $this->userPass4, $this->opt);

      return $pdo5;
      }
      }









      share|improve this question







      New contributor




      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm just starting to learn OOP and I've setup a class to handle the database connections and wanted to check if I'm going about this the right way.



      My website is already using PDO for prepared statements but it's procedural code otherwise. The site connects to multiple databases and one of the databases has two connections for separate privileges.



      I have created a Dsn class which contains a method for each connection. My config file instantiates the class and opens all the connections using the methods and passing params where necessary. I'll put the code below but just wanted to check if connecting to multiple databases in this way through a single class follows best practices for OOP.



      config.php



      $dbh = new Dsn;
      $pdo = $dbh->connnect1Read(NAME1, USER2, PASS2);
      $pdo2 = $dbh->connnect2Write(NAME1, USER1, PASS1);
      $pdo3 = $dbh->connnect3(USER2, PASS2);
      $pdo4 = $dbh->connnect4(USER3, PASS3);
      $pdo5 = $dbh->connnect5(USER4, PASS4);


      dsn.php



          class Dsn
      {

      private $host = 'localhost';
      private $charset = 'utf8mb4';

      private $dbName1;
      private $userName1;
      private $userPass1;

      private $dbName2 = 'db2';
      private $userName2;
      private $userPass2;

      private $dbName3 = 'db3';
      private $userName3;
      private $userPass3;

      private $dbName4 = 'db4';
      private $userName4;
      private $userPass4;


      private $opt = [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES => false,
      ];


      public function connnect1Read($dbName, $userName, $userPass)
      {
      $this->dbName1 = $dbName;
      $this->userName2 = $userName;
      $this->userPass2 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
      $pdo1 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

      return $pdo1;
      }

      public function connnect2Write($dbName1, $userName, $userPass)
      {
      $this->dbName1 = $dbName1;
      $this->userName1 = $userName;
      $this->userPass1 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName1 . ";charset=" . $this->charset . "";
      $pdo2 = new PDO($dsn, $this->userName1, $this->userPass1, $this->opt);

      return $pdo2;
      }

      public function connnect3($userName, $userPass)
      {
      $this->userName2 = $userName;
      $this->userPass2 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName2 . ";charset=" . $this->charset . "";
      $pdo4 = new PDO($dsn, $this->userName2, $this->userPass2, $this->opt);

      return $pdo3;
      }

      public function connnect4($userName, $userPass)
      {
      $this->userName3 = $userName;
      $this->userPass3 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName3 . ";charset=" . $this->charset . "";
      $pdo5 = new PDO($dsn, $this->userName3, $this->userPass3, $this->opt);

      return $pdo4;
      }

      public function connnect5($userName, $userPass)
      {
      $this->userName4 = $userName;
      $this->userPass4 = $userPass;

      $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->dbName4 . ";charset=" . $this->charset . "";
      $pdo3 = new PDO($dsn, $this->userName4, $this->userPass4, $this->opt);

      return $pdo5;
      }
      }






      php object-oriented pdo






      share|improve this question







      New contributor




      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Jordan Turner

      111




      111




      New contributor




      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Jordan Turner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.



          Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:



          function connnect($dbName, $userName, $userPass)
          {
          $host = 'localhost';
          $charset = 'utf8mb4';
          $opt = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
          ];
          $dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
          return new PDO($dsn, $userName, $userPass, $opt);
          }


          and then create your connections:



          $dbRead = connnect(NAME1, USER2, PASS2);
          $dbWrte = connnect(NAME1, USER1, PASS1);
          $db3 = connnect(NAME3, USER2, PASS2);
          $db4 = connnect(NAME4, USER3, PASS3);
          $db5 = connnect(NAME5, USER4, PASS4);





          share|improve this answer























          • Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
            – Jordan Turner
            2 days ago










          • Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
            – Your Common Sense
            2 days ago










          • Alright, well I'll take that onboard. Thanks again for your feedback.
            – Jordan Turner
            2 days ago










          • OOP will be involved in how you're going to use these connections though.
            – Your Common Sense
            2 days ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Jordan Turner is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207798%2foop-and-connecting-to-multiple-databases-using-pdo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.



          Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:



          function connnect($dbName, $userName, $userPass)
          {
          $host = 'localhost';
          $charset = 'utf8mb4';
          $opt = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
          ];
          $dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
          return new PDO($dsn, $userName, $userPass, $opt);
          }


          and then create your connections:



          $dbRead = connnect(NAME1, USER2, PASS2);
          $dbWrte = connnect(NAME1, USER1, PASS1);
          $db3 = connnect(NAME3, USER2, PASS2);
          $db4 = connnect(NAME4, USER3, PASS3);
          $db5 = connnect(NAME5, USER4, PASS4);





          share|improve this answer























          • Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
            – Jordan Turner
            2 days ago










          • Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
            – Your Common Sense
            2 days ago










          • Alright, well I'll take that onboard. Thanks again for your feedback.
            – Jordan Turner
            2 days ago










          • OOP will be involved in how you're going to use these connections though.
            – Your Common Sense
            2 days ago















          up vote
          2
          down vote













          This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.



          Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:



          function connnect($dbName, $userName, $userPass)
          {
          $host = 'localhost';
          $charset = 'utf8mb4';
          $opt = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
          ];
          $dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
          return new PDO($dsn, $userName, $userPass, $opt);
          }


          and then create your connections:



          $dbRead = connnect(NAME1, USER2, PASS2);
          $dbWrte = connnect(NAME1, USER1, PASS1);
          $db3 = connnect(NAME3, USER2, PASS2);
          $db4 = connnect(NAME4, USER3, PASS3);
          $db5 = connnect(NAME5, USER4, PASS4);





          share|improve this answer























          • Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
            – Jordan Turner
            2 days ago










          • Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
            – Your Common Sense
            2 days ago










          • Alright, well I'll take that onboard. Thanks again for your feedback.
            – Jordan Turner
            2 days ago










          • OOP will be involved in how you're going to use these connections though.
            – Your Common Sense
            2 days ago













          up vote
          2
          down vote










          up vote
          2
          down vote









          This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.



          Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:



          function connnect($dbName, $userName, $userPass)
          {
          $host = 'localhost';
          $charset = 'utf8mb4';
          $opt = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
          ];
          $dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
          return new PDO($dsn, $userName, $userPass, $opt);
          }


          and then create your connections:



          $dbRead = connnect(NAME1, USER2, PASS2);
          $dbWrte = connnect(NAME1, USER1, PASS1);
          $db3 = connnect(NAME3, USER2, PASS2);
          $db4 = connnect(NAME4, USER3, PASS3);
          $db5 = connnect(NAME5, USER4, PASS4);





          share|improve this answer














          This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.



          Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:



          function connnect($dbName, $userName, $userPass)
          {
          $host = 'localhost';
          $charset = 'utf8mb4';
          $opt = [
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES => false,
          ];
          $dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
          return new PDO($dsn, $userName, $userPass, $opt);
          }


          and then create your connections:



          $dbRead = connnect(NAME1, USER2, PASS2);
          $dbWrte = connnect(NAME1, USER1, PASS1);
          $db3 = connnect(NAME3, USER2, PASS2);
          $db4 = connnect(NAME4, USER3, PASS3);
          $db5 = connnect(NAME5, USER4, PASS4);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago









          Sᴀᴍ Onᴇᴌᴀ

          7,67561748




          7,67561748










          answered 2 days ago









          Your Common Sense

          3,186526




          3,186526












          • Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
            – Jordan Turner
            2 days ago










          • Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
            – Your Common Sense
            2 days ago










          • Alright, well I'll take that onboard. Thanks again for your feedback.
            – Jordan Turner
            2 days ago










          • OOP will be involved in how you're going to use these connections though.
            – Your Common Sense
            2 days ago


















          • Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
            – Jordan Turner
            2 days ago










          • Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
            – Your Common Sense
            2 days ago










          • Alright, well I'll take that onboard. Thanks again for your feedback.
            – Jordan Turner
            2 days ago










          • OOP will be involved in how you're going to use these connections though.
            – Your Common Sense
            2 days ago
















          Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
          – Jordan Turner
          2 days ago




          Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections.
          – Jordan Turner
          2 days ago












          Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
          – Your Common Sense
          2 days ago




          Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize
          – Your Common Sense
          2 days ago












          Alright, well I'll take that onboard. Thanks again for your feedback.
          – Jordan Turner
          2 days ago




          Alright, well I'll take that onboard. Thanks again for your feedback.
          – Jordan Turner
          2 days ago












          OOP will be involved in how you're going to use these connections though.
          – Your Common Sense
          2 days ago




          OOP will be involved in how you're going to use these connections though.
          – Your Common Sense
          2 days ago










          Jordan Turner is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Jordan Turner is a new contributor. Be nice, and check out our Code of Conduct.













          Jordan Turner is a new contributor. Be nice, and check out our Code of Conduct.












          Jordan Turner is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207798%2foop-and-connecting-to-multiple-databases-using-pdo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin