Exemplo n.º 1
0
 public static function Init($sServer, $sUser, $sPwd, $sSource = '')
 {
     self::$m_sDBHost = $sServer;
     self::$m_sDBUser = $sUser;
     self::$m_sDBPwd = $sPwd;
     self::$m_sDBName = $sSource;
     self::$m_oMysqli = null;
     mysqli_report(MYSQLI_REPORT_STRICT);
     // *some* errors (like connection errors) will throw mysqli_sql_exception instead
     // of generating warnings printed to the output but some other errors will still
     // cause the query() method to return false !!!
     try {
         $aConnectInfo = explode(':', self::$m_sDBHost);
         if (count($aConnectInfo) > 1) {
             // Override the default port
             $sServer = $aConnectInfo[0];
             $iPort = (int) $aConnectInfo[1];
             self::$m_oMysqli = new mysqli($sServer, self::$m_sDBUser, self::$m_sDBPwd, '', $iPort);
         } else {
             self::$m_oMysqli = new mysqli(self::$m_sDBHost, self::$m_sDBUser, self::$m_sDBPwd);
         }
     } catch (mysqli_sql_exception $e) {
         throw new MySQLException('Could not connect to the DB server', array('host' => self::$m_sDBHost, 'user' => self::$m_sDBUser), $e);
     }
     if (!empty($sSource)) {
         try {
             mysqli_report(MYSQLI_REPORT_STRICT);
             // Errors, in the next query, will throw mysqli_sql_exception
             self::$m_oMysqli->query("USE `{$sSource}`");
         } catch (mysqli_sql_exception $e) {
             throw new MySQLException('Could not select DB', array('host' => self::$m_sDBHost, 'user' => self::$m_sDBUser, 'db_name' => self::$m_sDBName), $e);
         }
     }
 }