通过代码将PHP连接到MySQL

时间:2011-01-25 00:01:05

标签: 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'];

    }
}

设置构造函数后,它会尝试通过connect方法

连接数据库
 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"

我在.ini文件中做错了什么使得它无法连接?

为什么这个if语句没有返回true:

if (isset($this->config['database']['username']) && isset($this->config['database']['password']))

3 个答案:

答案 0 :(得分:2)

嗯...... maby,因为你在你的ini文件中注释了用户名?

尝试将;username = "dbusername"更改为username = "dbusername"

说实话,并没有真正关注代码的其余部分,因为它非常多。 maby你可以尝试将它减少到重现问题所必需的基本部分吗?

然而,我上面提到的是我第一眼就看到了

答案 1 :(得分: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"

答案 2 :(得分:0)

我猜测前导分号(;)在ini文件中充当注释标记。因此,您的用户名和密码已被注释掉。