I have a .ini file for a configuration of my database and a constructor as follows:
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'];
}
}
After the constructor is set, it tries to connect with the database through the connect 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;
}
}
Here’s a part of my .ini file config:
[database]
type = "mysql"
;type = "postgresql"
server = "neighborme.db.7346057.hostedresource.com"
database = "neighborme"
;username = "dbusername"
;password = "dbpassword"
foreignKeyPostfix = "_uid"
am I doing something wrong in the .ini file that makes it so that it can’t connect?
Why is this if statement not returning true:
if (isset($this->config['database']['username']) && isset($this->config['database']['password']))
well… maby because you have commented out username in your ini file?
try changing
;username = "dbusername"tousername = "dbusername"to be honest didnt really look much at the rest of the code because its so much.
maby you can try to reduce it to the essential parts necessary to reproduce the problem?
however what i mentioned above was what i saw at first sight