它不应该是空变量

时间:2013-10-09 17:32:55

标签: php variables mysqli scope

我有一个数据库类,它是从模型扩展而来的。我尝试将数据库文件包含到成功连接到数据库,但变量为空(null)。有谁知道为什么?

这是我的数据库文件:

<?php

//ADATBAZIS
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_base = 'amgcgames';
$db_port = NULL;
$db_sock = NULL;

这是我的数据库文件:

<?php

class Database extends Model
{

    private $connection;
    private $query;
    private $result;
    private $file;
    private $db_host;
    private $db_user;
    private $db_pass;
    private $db_base;
    private $db_port;
    private $db_sock;

    function __construct()
    {
        parent::__construct();
        $this->file = 'application/config/database.php';
        if(is_readable($this->file))
        {
            require $this->file;

            var_dump($db_base);

            $this->db_host = $db_host;
            $this->db_user = $db_user;
            $this->db_pass = $db_pass;
            $this->db_base = $db_base;
            $this->db_port = $db_port;
            $this->db_sock = $db_sock;


            $this->connect();
        }
        else
        {
            die('Hiányzó adatbáziskonfikuráció: '.$this->file);
        }
    }

    function __destruct()
    {
        if(is_readable($this->file))
        {
            $this->close();    
        }
        else
        {
            die();
        }
    }

    public function connect()
    {
        $this->connection = new mysqli($this->db_host, $this->db_users, $this->db_pass, $this->db_base, $this->db_port, $this->db_sock);

        if($this->connection->connect_errno)
        {
            die('HIBA');
        }

        $this->connection->set_charset("utf8");
        return true;
    }

    public function close()
    {
        $this->connection->close();
        return true;
    }

    public function query($query)
    {
        $this->query = $query;
        if(isset($this->query))
        {
            $this->result = $this->connection->query($this->query);
        }
        return false;
    }

    public function escape($data)
    {
        return $this->connection->real_escape_string($data);
    }

    public function fetch($type = 'object')
    {
        if(isset($this->result))
        {
            switch($type)
            {
                case 'array':
                    //fetch a row as array
                    $row = $this->result->fetch_array();
                break;
                case 'assoc':
                    //fetch a row as array
                    $row = $this->result->fetch_assoc();
                break;
                default:
                    //fetch a row as object
                    $row = $this->result->fetch_object();    
                break;
            }
            return $row;
        }
        return FALSE;
    }
}

4 个答案:

答案 0 :(得分:1)

我刚刚运行了您的代码,var_dump($db_base)打印了正确的值。我假设您关闭了错误,因为您的连接方法中存在拼写错误:

$this->connection = new mysqli($this->db_host, $this->db_users, $this->db_p...);
-------------------------------------------------------------^ here

错误是:

  

PHP注意:未定义的属性:filename :: $ db_users in filename.php

您键入了$this->db_users而不是$this->db_user

您可能遇到的另一个问题可能是文件路径。你确定它是正确的吗?因为如果是,你应该在你的班级中提供你的变量。

答案 1 :(得分:0)

由于范围,变量未在构造函数中设置,您需要以某种方式将这些变量传递给类。您可以将它们全局化,但最好创建一个数组并将数组传递给构造函数。

像这样创建构造函数:

function __construct( $params ) {
    $this->db_host = $params['db_host'];
    $this->db_user = $params['db_user'];
    $this->db_pass = $params['db_pass'];
    $this->db_base = $params['db_base'];
    $this->db_port = $params['db_port'];
    $this->db_sock = $params['db_sock'];
}

然后像这样调用'模型':

$params = array(
    'db_host' => 'localhost',
    'db_user' => 'root',
    'db_pass' => '',
    'db_base' => 'amgcgames',
    'db_port' => NULL,
    'db_sock' => NULL,
)

$db = new Database( $params );

答案 2 :(得分:-2)

在函数外部定义的变量不可在其中访问。

defining视为常量,这将使它们可以访问。

答案 3 :(得分:-2)

配置文件中的变量在全局上下文中可见,因此您需要“通知”解释器,您希望在方法中使用它们。为此,请在需要行后添加global $db_user, $db_host, $db_pass, $db_base, $db_port, $db_sock;行。

或者像其他建议一样使用常量 - 事实上它在处理配置时更好的方法,但我将全局作为对这些变量不可见的原因的广泛解释。