Ejemplo n.º 1
0
 private function __construct($base, $param = null)
 {
     $this->_error = false;
     //polaczenie bez dodatkowych parametrow
     if ($param == null) {
         try {
             self::$_db = new mysqli($base['server'], $base['user'], $base['password']);
             self::$_baseName = $base['database'];
             if (mysqli_connect_errno()) {
                 $this->_error = true;
                 throw new Exception('Błąd podstawowego połączenia z baza DB: ' . mysqli_connect_errno() . '  :: ' . mysqli_connect_error());
             }
         } catch (Exception $e) {
             if (Manager_Config::isDev()) {
                 $this->showErrorsDB($e->getMessage());
             }
             return false;
         }
         if (false === self::$_db->select_db(self::$_baseName)) {
             $this->_error = true;
             return $this->throwErrorDB(null, 'Błąd nie wybrano bazy DB: ' . self::$_baseName . '.');
         }
         //parametry dodatkowe
     } elseif (is_array($param)) {
         $this->_db = mysqli_init();
         if (!is_object(self::$_db)) {
             $this->_error = true;
             return $this->throwErrorDB(null, 'Błąd zainicjowania obiektu mysqli.');
         }
         /****   parametry dodatkowe ***********
            MYSQLI_OPT_CONNECT_TIMEOUT - connection timeout in seconds
            MYSQLI_OPT_LOCAL_INFILE - enable/disable use of LOAD LOCAL INFILE
            MYSQLI_INIT_COMMAND - command to execute after when connecting to MySQL server
            MYSQLI_READ_DEFAULT_FILE - Read options from named option file instead of my.cnf
            MYSQLI_READ_DEFAULT_GROUP - Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE.
            */
         if (isset($param['options']) && is_array($param['options'])) {
             $size = count($param['options']);
             for ($i = 0; $i < $size; $i++) {
                 $res = $this->_db->options($param['options'][$i]['name'], $param['options'][$i]['value']);
                 if (true !== $res) {
                     $this->_error = true;
                     return $this->throwErrorDB(null, 'Błąd nadania dodatkowych opcji dla bazy DB.');
                 }
             }
         }
         /****   parametry dodatkowe ***********
            MYSQLI_CLIENT_COMPRESS - Use compression protocol
            MYSQLI_CLIENT_FOUND_ROWS - return number of matched rows, not the number of affected rows
            MYSQLI_CLIENT_IGNORE_SPACE - Allow spaces after function names. Makes all function names reserved words.
            MYSQLI_CLIENT_INTERACTIVE - Allow interactive_timeout seconds (instead of wait_timeout seconds) of inactivity before closing the connection
            MYSQLI_CLIENT_SSL - Use SSL (encryption)
            np.  mysql::getInstance( array('port'=> 3306, 'socket'=>null, 'flags' =>MYSQLI_CLIENT_SSL, 'options' => array ( array( 'name'=>MYSQLI_OPT_CONNECT_TIMEOUT, 'value'=> 120)  ) ) ); dziala
            */
         $port = isset($param['port']) ? $param['port'] : 3306;
         $socket = isset($param['socket']) ? $param['socket'] : null;
         $flags = isset($param['flags']) ? $param['flags'] : null;
         self::$_db->real_connect($base['server'], $base['user'], $base['password'], self::$_baseName, $port, $socket, $flags);
         if (mysqli_connect_errno()) {
             $this->_error = true;
             $this->throwErrorDB(null, 'Błąd połączenia z baza DB: z dodatkowymi parametrami');
             return false;
         }
     } else {
         //gdy $param ma nieprawidlowa postac
         $this->_error = true;
         $this->showErrorsDB('nieprawidłowe parametry dla bazy DB podano: ' . $param);
         return false;
     }
     //latin2 - ISO 8859-2 Central European
     //utf8 - UTF-8 Unicode
     if (false === self::$_db->set_charset($base['charset'])) {
         return $this->throwErrorDB(null, 'Błąd nadania kodowania: ' . $base['charset'] . ' dla bazy DB.');
     }
 }