2017-06-27 96 views
1

這裏是我的的config.php調用未定義的方法在PHP

<?php 

    define('DB_HOST', 'localhost'); 
    define('DB_NAME', 'xxxx'); 
    define('DB_USER', 'xxxx'); 
    define('DB_PASS', 'xxxx'); 
    ?> 

,它是db.php中

<?php 
include 'config.php'; 


class DB { 
    public static $pdo; 

    public static function connection(){ 

     if (!isset(self::$pdo)) { 

      try { 

      self::$pdo = new PDO('mysql:host='.DB_HOST.'; dbname ='.DB_NAME,DB_USER, DB_PASS); 
      }catch(PDOException $e){ 
       echo $e->getMessage(); 
      } 

     } 
     return self::$pdo; 
    } 


    public static function prepareOwn($sql){ 

     return self::connection()->prepare($sql); 
    } 
} 



?> 

第三文件是Student.php

<?php 
    include 'DB.php'; 



    class Student { 
     public $table = 'student_info'; 

     public function readAll(){ 
      $sql = "SELECT * FROM $this->table"; 

      $stmt = DB::prepareOwn($sql); 
      $stmt->execute(); 
      return $stmt->fetchAll(); 
     } 
    } 
?> 

但當我嘗試從使用的index.php spl_autoload_register()訪問readAll()然後我可以看到致命錯誤:調用未定義的方法DB :: prepareOwn()

誰能幫我解決問題??

非常感謝。 Sahidul

+0

你有沒有嘗試添加一個斷點,步進扔的代碼?當你啓動realAll時,好奇DB是什麼。 – Swordfish0321

+0

不,我沒有。你可以給我源實現斷點調試問題?? –

回答

1

我複製你的代碼到我的,看到你的錯誤。但正如我猜到了,首先你會得到一個錯誤這一行裏面db.php中:

return self::$pdo->prepare($sql); 

Fatal error: Call to a member function prepare() on null

其中準備功能來自? $ pdo只是這個類中的一個靜態屬性,它沒有一個叫做prepare的函數!修復此行

更新
問題是您忘記了在prepareOwn中調用連接方法。因此新prepareOwn功能應該是:

public static function prepareOwn($sql) { 
    self::connection(); 
    return self::$pdo->prepare($sql); 
} 
+0

我在try catch塊中實例化了self :: $ pdo = new PDO。 –

+0

此外,如果我使用空白prepareOwn()方法 –

+0

這是行不通的。確定它將'返回self :: $ pdo->連接($ sql);'但仍然是相同的問題 –

0

我希望這段代碼爲你工作

class MySQLDatabase { 

    // Class attributes 
    private $host_name = "localhost"; 
    private $database_name = "XXXXXXX"; 
    private $database_username = "XXXXXXX"; 
    private $database_password = "XXXXXXX"; 
    private $is_connected; 
    private $connection; 

    private $statement ; 


    // construct 
    public function __construct() { 

     $this->open_connection(); 

    }// End of construct 

    // connection method 
    public function open_connection() { 

     try { 
      $this->is_connected = TRUE ; 
      // PDO Connection 
      $this->connection = new PDO("mysql:host=".$this->host_name.";dbname=".$this->database_name.";charset=utf8",$this->database_username,$this->database_password); 
      // Error reporting 
      $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,FALSE); 

     } catch(PDOException $errors) { 

      $this->is_connected = FALSE ; 

      self::catch_errors($errors); 
     } 



    }// End of open connection method 

    // Get connection method 
    public function connection(){ 
     return $this->connection ; 
    } 

    // Close connection method 
    public function close_connection() { 

     $this->connection = null; 

    }// End of close connection method 

    private static function catch_errors($errors) { 
     echo("<h4><p>" . $errors -> getMessage() . "</p></h4>"); 
     die(); 
    } 


    // query method 

    public function query($sql){ 

    return $this->statement = $this->connection->prepare($sql); 

    } 




}// End of database class 

$database = new MySQLDatabase(); 





    class Student { 

     protected static $table = 'My_table'; 

     public function readAll(){ 
      global $database;  
      try{ 

       $sql = "SELECT * FROM ". self::$table; 
       $stmt = $database->query($sql); 
       $stmt->execute(); 
       return $stmt; 

      }catch(PDOException $error){ 

       echo("<h4><p>" . $errors -> getMessage() . "</p></h4>"); 
       die(); 

      } 

     } 
    } 
    $c = new Student();  
    $s = $c->readAll(); 
    $stmt = $s->fetchAll(PDO::FETCH_ASSOC); 

    foreach($s as $v){ 

     var_dump($v); 

    } 
相關問題