protected function connect($name) { if (array_key_exists('host', $this->_connections[$name]['connection_data'])) { $host = $this->_connections[$name]['connection_data']['host']; } else { $host = 'localhost'; } if (array_key_exists('port', $this->_connections[$name]['connection_data'])) { $port = $this->_connections[$name]['connection_data']['port']; } else { $port = self::DEFAULT_MYSQL_PORT; } if (array_key_exists('user', $this->_connections[$name]['connection_data'])) { $user = $this->_connections[$name]['connection_data']['user']; } else { $user = ''; } if (array_key_exists('password', $this->_connections[$name]['connection_data'])) { $password = $this->_connections[$name]['connection_data']['password']; } else { $password = ''; } if (array_key_exists('dbname', $this->_connections[$name]['connection_data'])) { $dbname = $this->_connections[$name]['connection_data']['dbname']; } else { $dbname = null; } $this->_connections[$name]['connected'] = true; try { $dsn = "mysql:host={$host}"; if ($port !== null) { $dsn .= ";port={$port}"; } if ($dbname !== null) { $dsn .= ";dbname={$dbname}"; } $dbh = new MyPDO($dsn, $user, $password, array()); } catch (PDOException $e) { throw new DB_Connection_Failure_Exception($name); } $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); $dbh->exec('SET CHARACTER SET utf8'); $this->_connections[$name]['pdo'] = $dbh; }
/** @fn dbconn($fnConfirm=$GLOBALS["dbConfirmFn"]) @param fnConfirm fn(dbConnectionString), 如果返回false, 则程序中止退出。 @key dbConfirmFn 连接数据库前回调。 连接数据库 数据库由全局变量$DB(或环境变量P_DB)指定,格式可以为: host1/carsvc (无扩展名,表示某主机host1下的mysql数据库名;这时由 全局变量$DBCRED 或环境变量 P_DBCRED 指定用户名密码。 dir1/dir2/carsvc.db (以.db文件扩展名标识的文件路径,表示SQLITE数据库) 环境变量 P_DBCRED 指定用户名密码,格式为 base64(dbuser:dbpwd). */ function dbconn($fnConfirm = null) { global $DBH; if (isset($DBH)) { return $DBH; } global $DB, $DBCRED, $USE_MYSQL; // e.g. P_DB="../carsvc.db" if (!$USE_MYSQL) { $C = ["sqlite:" . $DB, '', '']; } else { // e.g. P_DB="115.29.199.210/carsvc" // e.g. P_DB="115.29.199.210:3306/carsvc" if (!preg_match('/^"?(.*?)(:(\\d+))?\\/(\\w+)"?$/', $DB, $ms)) { throw new MyException(E_SERVER, "bad db=`{$DB}`", "未知数据库"); } $dbhost = $ms[1]; $dbport = $ms[3] ?: 3306; $dbname = $ms[4]; list($dbuser, $dbpwd) = getCred($DBCRED); $C = ["mysql:host={$dbhost};dbname={$dbname};port={$dbport}", $dbuser, $dbpwd]; } if ($fnConfirm == null) { @($fnConfirm = $GLOBALS["dbConfirmFn"]); } if ($fnConfirm && $fnConfirm($C[0]) === false) { exit; } try { $DBH = new MyPDO($C[0], $C[1], $C[2]); } catch (PDOException $e) { $msg = $GLOBALS["TEST_MODE"] ? $e->getMessage() : "dbconn fails"; throw new MyException(E_DB, $msg, "数据库连接失败"); } if ($USE_MYSQL) { $DBH->exec('set names utf8'); } $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # by default use PDO::ERRMODE_SILENT # enable real types (works on mysql after php5.4) # require driver mysqlnd (view "PDO driver" by "php -i") $DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $DBH->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); return $DBH; }