0

I am creating one database wrapper class and want to have a feature to connect with multiple database using PDO. I have connected multiple database using pdo as below.

 $config = array(
            'database1' => array(
                        'hostname' => 'localhost',
                        'username' => 'root',
                        'password' => '',
                        'database' => 'users',                    
                        'port'     => '', 
                        'dbtype'   => 'mysql'
            ),    
            'database2' => array(
                        'hostname' => 'localhost',
                        'username' => 'root',
                        'password' => '',
                        'database' => 'directory',                    
                        'port'     => '', 
                        'dbtype'   => 'mysql'
            ),

       );


    foreach ($config as $key => $value) 
    {

      $this->conn[$value['database']] = new PDO("mysql:host=".$value['hostname'].";dbname=".$value['database'], $value['username'],$value['password']); 
    }

Is it good practice to connect multiple databases as above using any types of database (may using Oracle or Mysql or may be both same). Because my above code takes more time in loading.

I want to have a best practice as Yii or Symphony framework uses to connect db.

Can anybody have solution.

Thanks

appsntech
  • 722
  • 1
  • 6
  • 21
  • Du you realize that it is not the code that causes more time in loading? – Your Common Sense Jul 01 '13 at 13:27
  • You have `mysql` hard coded in the connection string, so your code is only good for multiple MySQL connections. – MrCode Jul 01 '13 at 13:28
  • I library allows to connect multiple database may be two or more different oracle db or mysql. Above code is just a sample. There are more libraries to take care of queries i have used. I want something like popular framework uses @MrCode – appsntech Jul 01 '13 at 13:32
  • All the frameworks are giving their codes away. Why not to look then? But I still can't get why this code doesn't suit you. What's wrong with it? – Your Common Sense Jul 01 '13 at 13:34
  • I feel like if inside foreach I am trying to connect with db it may slow down the process. And passing those connection obj to another class which is extended and used in Query builder library. So I am looking for best architecture for database connection and query builder which can give max performance... I just not worrying about output, need a better performance with output.. So looking for better DB architecture library...If you can suggest any @Your Common Sense – appsntech Jul 01 '13 at 13:39
  • How can foreach slow down anything? How do you suppose to loop over your config array without foreach? Do you have any certain trouble now? Do you have an idea on *profiling*? – Your Common Sense Jul 01 '13 at 13:44
  • Hope you know we can avoid some bad practices like writing insert,update queries inside the foreach loop and likewise there are several ways foreach loops will slowdown the applications. So if you have any best Database Library architecture to implement then please let me know @Your Common Sense – appsntech Jul 01 '13 at 14:09
  • Who told you such a rubbish? **Using loops is not a bad practice at all**. – Your Common Sense Jul 01 '13 at 14:24
  • Anyways I just found it in some article. Let me know with some sample best architecture to create enterprise database class library if you have any. @Your Common Sense – appsntech Jul 01 '13 at 14:41
  • You know, to create a "best architecture to create enterprise database" one need to learn for several years and then have some experience in creating applications. And no short answer can substitute all this effort. – Your Common Sense Jul 01 '13 at 14:43
  • Yes may be what you are saying it is right but for that no one ready to wait for long. I did created some libraries and small MVC framework structure But I am looking for some well experianced people to analyse and give some tips to improve it. If you don't mind can I have your gmail id. So that I can explain and share you what I did so far. Thanks in advance @Your Common Sense – appsntech Jul 01 '13 at 14:47
  • By the way, on a ["a best practice as Yii or Symphony framework uses"](https://github.com/yiisoft/yii/issues/2251) - it's exactly on connecting to several databases. – Your Common Sense Jul 01 '13 at 14:51
  • Yes those frameworks connects multiple databases. Even The small framework which I am creating also uses multiple db. Just want to have features and better optimised code to make peoples work better. Can you please share your gmail Id so that I can share with you couple of code. Let me know if am in right way. Else you can help me out by showing me the best way to go......@Your Common Sense – appsntech Jul 01 '13 at 15:01

0 Answers0