PHP連接到MySQL我有我的數據庫的結構的.ini文件,並構造如下:構造函數設置通過代碼
function PHPRestSQL($iniFile = 'phprestsql.ini') {
$this->config = parse_ini_file($iniFile, TRUE);
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD'])) {
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
$this->requestData = '';
$httpContent = fopen('php://input', 'r');
while ($data = fread($httpContent, 1024)) {
$this->requestData .= $data;
}
fclose($httpContent);
}
$urlString = substr($_SERVER['REQUEST_URI'], strlen($this->config['settings']['baseURL']));
$urlParts = explode('/', $urlString);
$lastPart = array_pop($urlParts);
$dotPosition = strpos($lastPart, '.');
if ($dotPosition !== FALSE) {
$this->extension = substr($lastPart, $dotPosition + 1);
$lastPart = substr($lastPart, 0, $dotPosition);
}
array_push($urlParts, $lastPart);
if (isset($urlParts[0]) && $urlParts[0] == '') {
array_shift($urlParts);
}
if (isset($urlParts[0])) $this->table = $urlParts[0];
if (count($urlParts) > 1 && $urlParts[1] != '') {
array_shift($urlParts);
foreach ($urlParts as $uid) {
if ($uid != '') {
$this->uid[] = $uid;
}
}
}
$this->method = $_SERVER['REQUEST_METHOD'];
}
}
後,試圖通過連接與數據庫連接方法
function connect() {
$database = $this->config['database']['type'];
require_once($database.'.php');
$this->db = new $database();
if (isset($this->config['database']['username']) && isset($this->config['database']['password'])) {
if (!$this->db->connect($this->config['database'])) {
trigger_error('Could not connect to server', E_USER_ERROR);
}
} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
$this->config['database']['username'] = $_SERVER['PHP_AUTH_USER'];
$this->config['database']['password'] = $_SERVER['PHP_AUTH_PW'];
if (!$this->db->connect($this->config['database'])) {
$this->unauthorized();
exit;
}
} else {
$this->unauthorized();
exit;
}
}
這是我的.ini文件配置的一部分:
[database]
type = "mysql"
;type = "postgresql"
server = "neighborme.db.7346057.hostedresource.com"
database = "neighborme"
;username = "dbusername"
;password = "dbpassword"
foreignKeyPostfix = "_uid"
我是執行s .ini文件中出現錯誤,導致它無法連接?
爲什麼是這個if語句不返回true:
if (isset($this->config['database']['username']) && isset($this->config['database']['password']))
也與.ini文件中有一個[設置] baseURL =「/ phprestsql」,我應該在這裏根據上面的構造函數 – aherlambang 2011-01-25 00:20:42