0

我收到以下错误:

Fatal error: Call to undefined method database::connect() in
/Applications/XAMPP/xamppfiles/htdocs/proyectoFinal/core/class.ManageDatabase.php 
on line 8

有谁知道发生了什么?该方法在类内部定义。这部分似乎是问题所在: $this->link = $conn->connect();

类如下:

<?php 

include_once('../config.php');

    class database{
        protected $db_conn;
        public $db_name = DB_NAME;
        public $db_host = DB_HOST;
        public $db_pass = DB_PASS;
        public $db_user = DB_USER;  
    }

    function connect(){
        try{
            $this->$db_conn = new PDO("mysql:host = 
                    $this->db_host;dbname=$this->db_name",
                    $this->db_user, $this->db_pass);
            return $this->db_conn;  
        }
        catch(PDOException $e)
        {
        return $e->getMessage();
        }
    }
?>

以下方法调用的方法:

<?php
    include_once('../core/class.ManageDatabase.php');
    $init = new ManageDatabase;

    $table_name = 'persona';
    $data = $init->getData($table_name);

    print_r($data);
?>
4

2 回答 2

1
class database{
    protected $db_conn;
    public $db_name = DB_NAME;
    public $db_host = DB_HOST;
    public $db_pass = DB_PASS;
    public $db_user = DB_USER;  
} // <-- end of class database

它确实没有任何方法。}如果您希望函数connect()成为其方法之一,我相信您应该移动它,并将其仅放在函数之后。

于 2013-11-10T01:30:08.613 回答
0

你已经关闭了你的课程,所以: function connect(){ /* */ }

超出对象范围。

class database{
        protected $db_conn;
        public $db_name = DB_NAME;
        public $db_host = DB_HOST;
        public $db_pass = DB_PASS;
        public $db_user = DB_USER;  
    } // Remove this and add it at the end of your class definition 

话虽如此,这database->connect();不是一个定义的方法。而是:

$Var = connect();

这将适用于当前设置

于 2013-11-10T01:32:01.037 回答