如何在包含的 PHP 脚本中访问数据库配置


How to access db config in a included PHP script?

我正在重写我的应用程序以使用CodeIgniter。目前我正在与数据库作斗争。我已经在config/databases.php中配置了数据库,但不知道如何在以传统的非 MVC 方式加载的脚本中访问它们。

login.php来看,我有:

require_once("application/views/pages/include/membersite_config.php");
if(isset($_POST['submitted']))
{
   if($fgmembersite->Login())
   {
        $fgmembersite->RedirectToURL("main");
   }
}

在所需的membersite_config.php

require_once("fg_membersite.php");

fg_membersite Login() 导致DBLogin()是:

function DBLogin()
{
    $this->connection = pg_connect("host=localhost port=5432 dbname=".$this->db->database." user=".$this->db->username." password=".$this->db->password."");

我收到错误: Undefined property: FGMembersite::$dbMessage: Trying to get property of non-object

Codeigniter 是一个 MVC 框架,所以恐怕你必须喜欢这种模式。最好的方法是重写你的函数,以便你可以在模型中使用它。

当然,您可以绕过它,但是当您重写应用程序时,最好正确操作。

通过查看您的代码,您在此代码中调用的变量似乎可能具有另一个名称/位置(如果我错了,请纠正我):

function DBLogin()
{
    $this->connection = pg_connect("host=localhost port=5432 dbname=".$this->db->database." user=".$this->db->username." password=".$this->db->password."");

也许不是打电话给$this->db->database而是你需要打电话给$this->db['database']或其他东西。如果您提供该类的其余源代码,我们也许能够进一步帮助您。