0

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']))

3 Answers 3

2

well... maby because you have commented out username in your ini file?

try changing ;username = "dbusername" to username = "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

Sign up to request clarification or add additional context in comments.

2 Comments

also in the .ini file there is a [settings] baseURL = "/phprestsql", what should I put in here based on the constructor above
probably the path where your program has its bootstrap
1

Try to remove commented username and password line in your config.ini

[database]
type = "mysql"
;type = "postgresql"
server = "neighborme.db.7346057.hostedresource.com"
database = "neighborme"
;username = "dbusername"
username = "dbusername"
;password = "dbpassword"
password = "dbpassword"
foreignKeyPostfix = "_uid"

Comments

0

I would guess the leading semicolons (;) act as comment markers in the ini file. So your username and password are commented out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.