Example #1
0
 public function __construct()
 {
     try {
         $this->settings = parse_ini_file(ROOT . "/app/config/config.ini");
         $dsn = 'mysql:dbname=' . $this->settings["dbname"] . ';host=' . $this->settings["host"] . '';
         $this->DB = new PDO($dsn, $this->settings["user"], $this->settings["password"]);
         $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $this->DB->exec('SET NAMES "utf8"');
     } catch (PDOException $e) {
         echo 'Невозможно подключиться к серверу баз данных.';
         exit;
     }
 }
Example #2
0
 /**
  * Add a database
  *	$name: unique name to access it later, or an array containing all the paramaters as an associative array
  *	$driver: driver name (mysql, mssql, ...) (optional)
  *	$location: location of the database (file/host) (optional)
  *	$dbName: name of the database (optional)
  *	$user: username (optional)
  *	$pass: password (optional)
  *	$port: port to access the database (optional)
  *
  */
 public function AddDB($framework, $name, $driver = null, $location = null, $dbName = null, $user = null, $pass = null, $port = null)
 {
     $dsn = null;
     $options = array(PDO::ATTR_PERSISTENT => true);
     $newDb = null;
     $skipCredentials = false;
     if (is_array($name)) {
         if (isset($name['port'])) {
             $port = $name['port'];
         }
         if (isset($name['pass'])) {
             $pass = $name['pass'];
         } else {
             if (isset($name['password'])) {
                 $pass = $name['password'];
             }
         }
         if (isset($name['user'])) {
             $user = $name['user'];
         } else {
             if (isset($name['username'])) {
                 $user = $name['username'];
             }
         }
         if (isset($name['dbName'])) {
             $dbName = $name['dbName'];
         } else {
             if (isset($name['db'])) {
                 $dbName = $name['db'];
             }
         }
         if (isset($name['location'])) {
             $location = $name['location'];
         } else {
             if (isset($name['host'])) {
                 $location = $name['host'];
             }
         }
         if (isset($name['driver'])) {
             $driver = $name['driver'];
         } else {
             if (isset($name['type'])) {
                 $driver = $name['type'];
             }
         }
         if (isset($name['name'])) {
             $name = $name['name'];
         } else {
             if (isset($name['identifier'])) {
                 $name = $name['identifier'];
             }
         }
     }
     if ($dbName == null) {
         throw new Exception("Please set the database name using the db/dbName parameter", 1);
     }
     if ($location == null) {
         throw new Exception("Please set the DB location using the location/host parameter", 1);
     }
     if ($driver == null) {
         throw new Exception("Please set the DB driver using the driver/type parameter", 1);
     }
     if ($name == null) {
         throw new Exception("Please set the DB name using the name/identifier parameter", 1);
     }
     switch ($driver) {
         case 'dblib':
         case 'mssql':
         case 'sybase':
         case 'mysql':
             if ($port == null) {
                 $dsn = "{$driver}:host={$location};dbname={$dbName}";
             } else {
                 if ($driver == 'mysql') {
                     $dsn = "{$driver}:host={$location};port={$port};dbname={$dbName}";
                 } else {
                     $dsn = "{$driver}:host={$location}:{$port};dbname={$dbName}";
                 }
             }
             if ($driver == 'mysql') {
                 $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
             }
             break;
         case 'sqlite':
         case 'sqlite2':
             // sqlite doesn't require user/pass/host/port
             $newDb = new DB("{$driver}:" . ($location == 'memory' ? ':memory:' : $location));
             break;
         case 'pgsql':
             $skipCredentials = true;
             $dsn = "pgsql:host={$location};";
             if ($port != null) {
                 $dsn .= "port={$port};";
             }
             $dsn .= "dbname={$dbName};";
             if ($user != null) {
                 $dsn .= "user={$user};";
                 if ($pass != null) {
                     $dsn .= "password={$pass};";
                 }
             }
             // @TODO
             return;
             break;
         default:
             if ($framework->debug) {
                 throw new Exception("Unsupported database driver", 1);
             }
             return null;
     }
     if ($newDb == null) {
         if ($user == null || $skipCredentials) {
             $newDb = new DB($dsn);
         } else {
             if ($pass == null) {
                 $newDb = new DB($dsn, $user);
             } else {
                 $newDb = new DB($dsn, $user, $pass);
             }
         }
     }
     $newDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $framework->db[$name] = $newDb;
     return $newDb;
 }
Example #3
0
 public static function setAttribute()
 {
     // Already an instance of this? Return, if not, create.
     $db = new DB();
     return $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
 }