Access design to MySql database [closed]

0

I've been investigating good implementations that allow easy adaptability to my db connection. I would like to receive feed backs and suggestions about improvements to this design. For example: how would you implement more than one database in this model, would it be better to use an abstract class for the database, if so, how would you do it? I am in a project that has several phases, in the future there may be a need to change / add DB's and tables and I do not want to have to redo the structure. File Contents:

config.php:

$db1Config = array(
   "dbName" => "master_db",
   "dbUser" => "master_user",
   "dbPassword" => "master_pass",
   "dbHost" => "master_host"
);

Class DB, DB.php:

class DB {

   private static $_instance = Null;
   private $_db = Null;

   public function __construct($dbConfig) {

      try {
         $this->_db = new PDO('mysql:host=' .$dbConfig["dbHost"]. ';dbname=' .$dbConfig["dbName"], $dbConfig["dbUser"], $dbConfig["dbPassword"]);
        $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      } catch (Exception $e) {
        $error = $e->getMessage();
        //echo $error;
        echo 'Updating database. Try again in a few moments. Sorry';
        die();
      }
   }

   public static function getInstance($dbConfig) {
      if(!isset(self::$_instance)) {
         self::$_instance = new DB($dbConfig);
      }
      return self::$_instance;
   }

   public function select_data($table, $where, $column, $upOrDown)
      ...
   }

   ...
}

Class Service, Service.php

class Service {

   private $_db = Null;

   public function __construct($dbConfig) {
      $this->_db = DB::getInstance($dbConfig);
   }

   public function service_exists($servId) {
      $servExists = $this->_db->select_data('services', array('id', '=', $servId), Null, Null);

      if (count($servExists) > 0) {
         return True;
      }

      return False;
   ...
}

index.php

$service = new Service($db1Config);
if($service->service_exists($servId) {
   ...
}
...
    
asked by anonymous 09.07.2015 / 12:47

1 answer

1

When analyzing your implementation, I see nothing wrong.

As for its adaptability in several cases I realize what it tries to do by having a base configuration array .

It is common to see in configurations implementations in PHP files, and even with the presence of GLOBAIS variables. However I have found situations in which system administrators do not understand PHP and in that sense and although it is not difficult, today as a professional programmer I understand that we should use some standards.

Whether it is for the future, or for us to pass organized forms on how to set up a system we have created. Today we are inside of what we do but tomorrow we do not remember. It is still important for the projects we produce that tomorrow others can handle the same in our absence.

So for configuration instead of an array in php code ... why not use for example an INI file.

Database user, passwords, etc ... are settings that can be changed frequently and should not be understood PHP to be able to modify this type of configuration.

As for your class logic, I do not see anything much but to help you more you have to be more concrete.

    
09.07.2015 / 16:13