Mysql database access design

Asked

Viewed 38 times

0

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

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) {
   ...
}
...
  • what is in your understanding "scalable for connection to my db"

  • Be able to use this design if there are more implementations to do, other connections, other DB, etc...

  • then edit your question and change the word for adaptability because a scalable database system is one that allows you to grow in number of databases or tables in order to answer in number of accesses.

  • Edited, sorry for the misunderstanding

  • don’t need to apologize just thought to get a good answer you need to be precise and not leave the doubt on who can help you.

  • Miguel, could you give an example, the question seems very comprehensive and or based on opinions, at the risk of being closed. Edit it by adding examples and situations where your code doesn’t fit.

Show 1 more comment

1 answer

1

When analysing its implementation, I see nothing wrong.

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

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

Whether for the future, or to pass organized forms regarding the configuration of a system we created. Today we are aware of what we do but tomorrow we no longer remember. It is also important for the projects that we produce that tomorrow others can mess with it in our absence.

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

Database user, your passsword, etc... are settings that can be changed with some frequency and should not be necessary to understand PHP to be able to modify this type of configuration.

As for your class logic I see nothing too much but to help you more you have to be more concrete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.