2011-01-25 76 views
0

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

回答

2

呃...马比,因为你在你的ini文件中注释掉了用户名?

尝试改变;username = "dbusername"username = "dbusername"

说实话真正地不看多的代码的其余部分,因为它的这么多。 maby你可以尝试将它减少到重现问题所必需的必要部分?

但是我上面提到的是我第一眼看到

+0

也与.ini文件中有一个[设置] baseURL =“/ phprestsql”,我应该在这里根据上面的构造函数 – aherlambang 2011-01-25 00:20:42

0

我猜领先的分号(;)作为在ini文件中注释标记。所以你的用户名和密码被注释掉了。

1

尝试删除评论的用户名和密码线在你的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"