示例#1
0
文件: core.php 项目: Slini11/okapi
 public static function connect()
 {
     if (mysql_connect(Settings::get('DB_SERVER'), Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'))) {
         mysql_select_db(Settings::get('DB_NAME'));
         mysql_query("set names '" . Settings::get('DB_CHARSET') . "'");
         self::$connected = true;
     } else {
         throw new Exception("Could not connect to MySQL: " . mysql_error());
     }
 }
示例#2
0
 public static function connect()
 {
     $dsnarr = array('host' => Settings::get('DB_SERVER'), 'dbname' => Settings::get('DB_NAME'), 'charset' => Settings::get('DB_CHARSET'));
     $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, PDO::MYSQL_ATTR_LOCAL_INFILE => true);
     /* Older PHP versions do not support the 'charset' DSN option. */
     if ($dsnarr['charset'] and version_compare(PHP_VERSION, '5.3.6', '<')) {
         $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $dsnarr['charset'];
     }
     $dsnpairs = array();
     foreach ($dsnarr as $k => $v) {
         if ($v === null) {
             continue;
         }
         $dsnpairs[] = $k . "=" . $v;
     }
     $dsn = 'mysql:' . implode(';', $dsnpairs);
     try {
         self::$dbh = new PDO($dsn, Settings::get('DB_USERNAME'), Settings::get('DB_PASSWORD'), $options);
     } catch (PDOException $e) {
         throw new DbException($e->getMessage());
     }
     self::$connected = true;
 }