2013-12-08 37 views
-1

我做了2文件的PHP:你能解釋一下這個錯誤信息嗎?

  1. class.datebase.php
  2. class.ManageDatabase.php

這個文件我用來連接到數據庫......但我得到的錯誤。 ..

你能幫助我..

class.database.php:

<?php 

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

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

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();   
    }   
} 

    ?> 

class.ManageDatabase.php:

<?php 

    class ManageDatabase{ 
     public $link; 

     function __construct(){ 
      include_once('class.database.php'); 
      $conn = new database; 
      $this->link = $conn->connect(); 
      return $this->link; 
     } 
    } 

    $something = new ManageDatabase; 
    echo $something; 
?> 

和錯誤:

Fatal error: Call to undefined method database::connect() in C:\xampp\htdocs\myweb\crud\core\class.ManageDatabase.php on line 9 
+1

連接()不是類數據庫中的一員。 – erenon

+0

謝謝你的...... 我是多麼傻啊:) – user2467583

回答

3

把連接功能的數據庫類,而不是出它(在這個文件:class.database.php)

0

函數connect定義在類數據庫之外

class database { 
    protected $db_conn; 
    public $db_name = DB_NAME; 
    public $db_host = DB_HOST; 
    public $db_user = DB_USER; 
    public $db_pass = DB_PASS;  
// don't close the class here 

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();   
    }   
} 
} //close it here 
+0

謝謝米格洛... – user2467583

0

簡單... 您在這個得變化:

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


} 

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();   
    } 

} 

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

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();   
     }   
    }  
} 

你的函數之前關閉您的類,所以當你叫裏面的功能類,它沒有工作。

+0

嗯.... 謝謝.... – user2467583

0

你連接功能,應該是類的方法:

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

    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();   
     }   
    } 
}